Thursday, February 19, 2009

Sometimes it is fun to goof around

#!/usr/bin/python

fiblist = [0, 1]

def fib(n):
  if(len(fiblist) < n):
    fiblist.append(fib(n-2) + fib(n-1))
  return fiblist[n-1]


Because sometimes it is more fun to goof around than to do homework.

My first version didn't keep a list, so the recursion got hairy quickly.  I tried fib(100) and waited long enough to expect an out-of-memory error for the VM stack rather than an answer.  The list version (shown) is as fast as the blink of an eye for fib(100), but it overflows to Long and I'm not sure whether Python retains significance or only retains magnitude.

I've always liked the fibonacci series.  When I read The DaVinci Code, I remember getting most of the mysteries right without cheating.  I think one or more had to do with this series, but I don't remember anymore.

In grade school math we had to draw a poster based a geometric theme.  I drew a series of circles whose centerpoints lied on a logarithmic spiral with diameters of the Fibonacci series filled with colors whose wavelengths approximated the Fibonacci numbers over the visible spectrum for the number of circles that fit on the poster.  It took a long time and a lot of thought.  I received a "C" because there "wasn't enough math."  In truth, the geometry was there but not perceptible if you didn't know where to look.

I liked my poster and I didn't care about the low marks.

Besides Dan Brown's Fibonacci parlor tricks, another source of "mainstream" Fibonacci goodness comes from Tool's song Lateralus.  The syllables in each line arpeggiate the Fibonacci Series.  There are tons of logarithmic easter eggs too, such as the lyrics starting 1.617 minutes into the song.  Swing on the spiral.