loading..
Ðóññêèé    English
17:33

About generation of number sequences in SQL Server page 2

B. About Fibonacci sequence

The Fibonacci sequence might be calculated by this iteration formula:

A[i+2] = A[i] + A[i+1];  i = 1,…, n; A[1] = A[2] = 1.

It`s simple to see the analogy with general case in previous point if we bring in function f(x,y)=x+y. The [iter] column is used for keeping iteration`s number, [a] column is used for keeping A[i], [b] and [c] for A[i+1] and A[i+2] correspondingly. The [d] column is not in use. The calculation of Fibonacci numbers may be put into code accordingly to general method like this:

Console
Execute
  1. WITH Fibonacci(iter,a,b,c) AS
  2. (
  3.  SELECT iter=1, a=1, b=1, c=1+1
  4.  UNION ALL
  5.  SELECT iter+1, a=b, b=c, c=b+c
  6.  FROM Fibonacci WHERE b < 1000
  7. )
  8. SELECT * FROM Fibonacci;

Let`s give the result of query for calculation Fibonacci numbers are less than 1000 (2nd column).

iter a b c
1    1    1    2
2    1    2    3
3    2    3    5
4    3    5    8
5    5    8    13
6    8    13    21
7    13    21    34
8    21    34    55
9    34    55    89
10    55    89    144
11    89    144    233
12    144    233    377
13    233    377    610
14    377    610    987
15    610    987    1597
16    987    1597    2584

C. Equation`s root finding

The large section of mathematical and functional analysis is dedicated to finding equations` roots of one-variable and multivariable functions. The root of equation g(x) = 0 is a number r (or vector r) which complies with condition g(r) = 0. The general method of solving such equations is in reduction to the problem of fixed point:

x=f(x).

The sense of this reduction is in the finding of such function f which makes equations g(x) = 0 and x = f(x) equivalent. Besides, operator f must be contracting. That is if the value r1 takes place near solution r, than the value r2 = f(r1) must be even nearer to the solution: abs(r-r2) < A * abs(r-r1), where positive constant A is less then unity (A<1) and isn`t depends on select of r1 value.

The contracting mappings are possible be many. The preference ones are that for which A constant takes less value. The less the constant the process of finding of equation`s g(x) root converges faster:

r2 = f(r1), r3 = f(r2),  r4 = f(r3) …
Let`s note an illustrating example in SQL.
Bookmark and Share
Pages 1 2 3
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.