Skip to content

Instantly share code, notes, and snippets.

@farseerfc
Created November 25, 2011 03:12
Show Gist options
  • Save farseerfc/1392749 to your computer and use it in GitHub Desktop.
Save farseerfc/1392749 to your computer and use it in GitHub Desktop.
3 kinds of python code for fibonacci
#!/usr/bin/python2
def fibs1(i):
fibs=[1,1]
for j in xrange(2,i):
fibs.append(fibs[j-1]+fibs[j-2])
return fibs
def fib2():
yield (1,1);
for f in fib2():
yield (f[1],f[0]+f[1])
def fibs2(i):
from itertools import islice
return map(lambda x:x[0],islice(fib2(),0,i))
def fib3(i):
if i <2: return 1
return fib3(i-1)+fib3(i-2)
def fibs3(i):
result=[]
for j in xrange(i):
result.append(fib3(j))
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment