Instantly share code, notes, and snippets.

# enigmaticape/sicp_1_13_pyfib.py Created Nov 4, 2012

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 "
\(",n,"\)\(",fib(n),"\)\(",goldfib(n),"\)\(",closed(n),"\)\(",psiroot(n),"\)
", \ "", \ "", \ "", \ "" n = n + 1 drawtable();
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/