Actionscript 2.0 Fibonacci Class

This 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.

Download Fibonacci Class

The Discussion

8 Comments on “Actionscript 2.0 Fibonacci Class”

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.


Huh?


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.


The indentation disappeared. ;/ Everything after ‘dev fibonacci():’ should be indented four spaces, everything after ‘while True:’ should be indented eight.


[...] Fibonacci generator [...]



this is much much much simpler

output=(”The fibonacci sequence is 0,1,1,”);
a=0;
b=1;
c=a+b;

while(c


Hey PJ, can you fix the link to the class please?


Comment Love

Leave a comment, have 50 cents donated to a charity.

RSS Feed

Feed your RSS reader

Subscribe to Some Random Dude. It's nutritious.