September 24, 2005

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

Posted by PJ at September 24, 2005 08:42 PM | TrackBack
Comments

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.

Posted by: Dark-forest at September 30, 2005 06:39 AM

Huh?

Posted by: Laurie at October 2, 2005 02:43 PM

I assume that this metod takes an argument for how long/deep you want the fibonacci, otherwise it coould be an endless loop.
It is the same with prime numbers. You don't make a class which calculates every prime number. They are endless.

Posted by: Patrick at January 4, 2006 01:14 AM

Of course. :)

Posted by: P.J. Onori at January 4, 2006 10:03 AM