Created
November 25, 2011 03:12
-
-
Save farseerfc/1392749 to your computer and use it in GitHub Desktop.
3 kinds of python code for fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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