If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Fibonacci Numbers (plain DB2)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-19-09, 11:35
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb 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:


Quote:
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
Lenny.
Reply With Quote
  #2 (permalink)  
Old 08-19-09, 14:56
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
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 !


Quote:
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
Lenny
Reply With Quote
  #3 (permalink)  
Old 08-20-09, 18:01
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
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; 08-21-09 at 12:38.
Reply With Quote
  #4 (permalink)  
Old 08-21-09, 12:33
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
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; 08-21-09 at 12:38.
Reply With Quote
  #5 (permalink)  
Old 08-21-09, 12:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
todd, you need to post this stuff on a maths forum, where there might actually be someone who cares...

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 08-21-09, 12:46
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Did you an SQL in the first two messages ?

Everybody can use them. But not everybody knows what does it mean.

Lenny
Reply With Quote
  #7 (permalink)  
Old 08-25-09, 23:53
wilsonfv wilsonfv is offline
Registered 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!
Reply With Quote
  #8 (permalink)  
Old 08-26-09, 09:51
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by wilsonfv
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!
This is just a recursive SQL statement. Nothing else.

Lenny
Reply With Quote
  #9 (permalink)  
Old 11-19-09, 11:14
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation 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
Quote:
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
Lenny
Reply With Quote
  #10 (permalink)  
Old 11-19-09, 12:55
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs up 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
Reply With Quote
  #11 (permalink)  
Old 11-19-09, 20:53
ElenaVas ElenaVas is offline
Registered 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; 11-19-09 at 21:14.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On