Actionscript 2.0 Fibonacci Class
24.09.05 @ 8:42 pmThis is about as simple as it gets, but I thought it may come to some use for someone out there.
If you’re unfamiliar with the Fibonacci sequence, here’s a short snippet from Wikipedia
The earliest known reference to Fibonacci numbers is contained in a book on meters by an Indian mathematician named Pingala called Chhandah-shastra (500 BC). As documented by Donald Knuth in The Art of Computer Programming, this sequence was described by the Indian mathematicians Gopala and Hemachandra in 1150, who were investigating the possible ways of exactly bin packing items of length 1 and 2. In the West, it was first studied by Leonardo of Pisa, who was also known as Fibonacci (c. 1200), to describe the growth of an idealized rabbit population. The numbers describe the number of pairs in the rabbit population after n months if it is assumed that
- in the first month there is just one new-born pair,
- new-born pairs become fertile from their second month on
- each month every fertile pair begets a new pair, and
- the rabbits never die
Suppose that in month n we have a pairs of fertile and newly born rabbits and in month n + 1 we have b pairs. In month n + 2 we will necessarily have a + b pairs, because all a pairs of rabbits from month n will be fertile and produce a pairs of offspring, while the newly born rabbits in b will not be fertile and will not produce offspring.
The equation goes as such:
F = n2 + n1
where n2 is the current number in the sequence and n1 is the number immediately preceding n2.
The equation produces the following sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, …
This sequence is surprisingly common in nature and has been applied in many mathematical equations. I’m really looking forward to using this for some sort of fractal experiment in Flash. Should be fun.

September 30th, 2005 at 6:39 am
$0.00 in Comment Love for October
it sounds like a multyplying thing that has no end (no factors that actualy die, right?) and can jam the computer if it’s performed in a computer.
October 2nd, 2005 at 2:43 pm
$0.00 in Comment Love for October
Huh?
February 24th, 2006 at 2:15 pm
$0.00 in Comment Love for October
Great example of where generators are incredibly useful: You don’t have to supply a ’stop here’ point of a sequence, you can continue in ‘inifinity’. Python example:
def fibonacci():
a, b = 0, 1
yield 0
while True:
yield b
a, b = b, a + b
And that’s all you need. Then you can do something like this:
fib = fibonacci()
fib.next() # 0
fib.next() # 1
fib.next() # 1
fib.next() # 2
fib.next() # 3
fib.next() # 5
… and so on.
February 24th, 2006 at 2:16 pm
$0.00 in Comment Love for October
The indentation disappeared. ;/ Everything after ‘dev fibonacci():’ should be indented four spaces, everything after ‘while True:’ should be indented eight.
April 13th, 2006 at 6:08 pm
$5.00 in Comment Love for October
[…] Fibonacci generator […]
August 29th, 2006 at 3:59 pm
$5.00 in Comment Love for October
[…] http://www.somerandomdude.net/blog/flash/actionscript-fibonacci-class/ […]
January 27th, 2008 at 7:35 pm
$0.00 in Comment Love for October
this is much much much simpler
output=(”The fibonacci sequence is 0,1,1,”);
a=0;
b=1;
c=a+b;
while(c