Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Various python code to do Fibonacci related computations
#!/usr/bin/python
# This is the python script I used to generate
# the table that illustrates my rambling answer
# to SICP exercise 1.13
import math
# Golden ratio and conjugate
phi = (1 + math.sqrt( 5 ) ) / 2
psi = (1 - math.sqrt( 5 ) ) / 2
# Linear recursive fib
def fib( n ):
if n == 0 : return 0
if n == 1 : return 1
return fib( n - 1 ) + fib( n - 2 )
# approximation by golden ration
def goldfib( n ):
return ( phi**n ) / math.sqrt( 5 )
# approximation of diff by psi
def psiroot( n ):
return abs( ( psi**n ) / math.sqrt( 5 ) )
# closed form fib
def closed( n ):
return ( (phi**n - psi**n) / math.sqrt( 5 ))
# chuck out an HTML table
def drawtable():
n = 0
while n < 20:
print "<tr><td>\(",n,"\)</td>", \
"<td>\(",fib(n),"\)</td>", \
"<td>\(",goldfib(n),"\)</td>", \
"<td>\(",closed(n),"\)</td>", \
"<td>\(",psiroot(n),"\)</td></tr>"
n = n + 1
drawtable();
@enigmaticape

This comment has been minimized.

Copy link
Owner Author

enigmaticape commented Nov 16, 2012

Used to generate the table of results that supports the solution to SICP exercise 1.13 at http://www.enigmaticape.com/blog/sicp-exercise-1-13/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.