Results 1 to 11 of 11
Thread: Fibonacci Numbers (plain DB2)

081909, 11:35 #1Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Unanswered: Fibonacci Numbers (plain DB2)
In mathematics, the Fibonacci numbers are the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, etc
By definition, the first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:
F(n) = F(n  1) + F(n  2) where F(n) is Fibonacci function.
with seed values: F(0) = 0 and F(1) = 1.
Why this is interesting for DB2 user ?
Because we have to remembering the 2 previous numbers to find the current number.
Of cause we can join the same table 3 times and solve this problem, but I want to give you the simplier solution:
with contol_tbl (max_no) as
(select int(46) from sysibm.sysdummy1)
,
Fibonacci_number (seq, Fbc, Fbck_1, Fbck_2, max_no) as
(
select int(1), int(0), int(1), int(0), max_no from contol_tbl
union all
select seq + 1, Fbck_1 + Fbck_2, Fbck_1 + Fbck_2, Fbck_1, max_no
from Fibonacci_number
where seq + 1 <= max_no
)
select
seq "Sequence Number", Fbc "Fibonacci Number"
from Fibonacci_number

081909, 14:56 #2Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
You can see:
Only 46 Fibonacci's numbers we can get if we'll use the integer representation of the column in the Fibonacci_number table.
Try to change the integer column to float, or double.
Instead of the 46 numbers we can generate 364 numbers.
Also not too much !
with contol_tbl (max_no) as
(select int(364) from sysibm.sysdummy1)
,
Fibonacci_number (seq, Fbc, Fbck_1, Fbck_2, max_no) as
(
select int(1), double(0), double(1), double(0), max_no from contol_tbl
union all
select seq + 1, Fbck_1 + Fbck_2, Fbck_1 + Fbck_2, Fbck_1, max_no
from Fibonacci_number
where seq + 1 <= max_no
)
select
seq "Sequence Number",
case
when seq <= 46
then case when strip(digits(int(Fbc)), L, '0') = ' '
then '0'
else strip(digits(int(Fbc)), L, '0') end
else lcase(varchar(Fbc))
end "Fibonacci Number"
from Fibonacci_number

082009, 18:01 #3Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
The bee ancestry code.
Fibonacci numbers also appear in the description of the reproduction of a population of idealized bees, according to the following rules:
If an egg is laid by an unmated female, it hatches a male.
If, however, an egg was fertilized by a male, it hatches a female.
Thus, a male bee will always have one parent, and a female bee will have two.
If one traces the ancestry of any male bee (1 bee), he has 1 female parent (1 bee). This female had 2 parents, a male and a female (2 bees). The female had two parents, a male and a female, and the male had one female (3 bees). Those two females each had two parents, and the male had one (5 bees). This sequence of numbers of parents is the Fibonacci sequence.
This is an idealization that does not describe actual bee ancestries. In reality, some ancestors of a particular bee will always be sisters or brothers, thus breaking the lineage of distinct parents.Last edited by Lenny77; 082109 at 12:38.

082109, 12:33 #4Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
A Fibonacci prime is a Fibonacci number that is prime .
The first few are:
2, 3, 5, 13, 89, 233, 1597, 28657, 514229, ...
Fibonacci primes with thousands of digits have been found, but it is not known whether there are infinitely many. They must all have a prime index, except F4 = 3. There are arbitrarily long runs of composite numbers and therefore also of composite Fibonacci numbers.
With the exceptions of 1, 8 and 144 (F1 = F2, F6 and F12) every Fibonacci number has a prime factor that is not a factor of any smaller Fibonacci number (Carmichael's theorem).
144 is the only nontrivial square Fibonacci number.
Attila Petho proved in 2001 that there are only finitely many perfect power Fibonacci numbers. In 2006, Y. Bugeaud, M. Mignotte, and S. Siksek proved that only 8 and 144 are perfect powers.Last edited by Lenny77; 082109 at 12:38.

082109, 12:43 #5SQL Consultant
 Join Date
 Apr 2002
 Location
 Toronto, Canada
 Posts
 20,002
todd, you need to post this stuff on a maths forum, where there might actually be someone who cares...

082109, 12:46 #6Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Did you an SQL in the first two messages ?
Everybody can use them. But not everybody knows what does it mean.
Lenny

082509, 23:53 #7Registered User
 Join Date
 Apr 2009
 Posts
 42
Wow! Thanks! Sure it will help in the future.
But I cannot fully understand the sytax of the sql.
Can you kindly share some doc of usage the 'with...as' ?
THX!

082609, 09:51 #8Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Originally Posted by wilsonfv
Lenny

111909, 11:14 #9Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
The simplified query
As usually, we can simplify expression without losing speed of reception of result:
Code:with contol_tbl (max_no) as (select 46 from sysibm.sysdummy1) , Fibonacci_number (seq, Fib, Fib1, max_no) as ( select 1, double(0), double(1), max_no from contol_tbl union all select seq + 1, Fib + Fib1, Fib, max_no from Fibonacci_number where seq + 1 <= max_no ) select seq "Sequence Number", case when seq <= 46 then case when strip(digits(int(Fib)), L, '0') = ' ' then '0' else strip(digits(int(Fib)), L, '0') end else lcase(varchar(Fib)) end "Fibonacci Number" from Fibonacci_number
Fibonacci Number
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170

111909, 12:55 #10Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Fyi
That is interesting: using this formula it is not possible to find more than 365 Fibonaccy numbers.
Last Fibonaccy number, find by this formula, to which we can be trusted is 806515533049393 (seq = 74) after it numbers are approximated
Lenny

111909, 20:53 #11Registered User
 Join Date
 Nov 2009
 Posts
 5
it's really interesting, thanks a lot!.
I have found another topic about the implementation this procedure at the forum.Last edited by ElenaVas; 111909 at 21:14.