Skip to content

Instantly share code, notes, and snippets.

@calvinfroedge
Created January 25, 2012 01:14
Show Gist options
  • Save calvinfroedge/1673958 to your computer and use it in GitHub Desktop.
Save calvinfroedge/1673958 to your computer and use it in GitHub Desktop.
Fibonacci implementation (python) - Can you find a more efficient way?
#Using a loop:
def f(n):
if n == 1 or n == 0 : return n
else:
x = 0
y = 1
r = 0
for i in range(n - 1):
r = x + y
x = y
y = r
return r
n = input("Enter an integer to calculate fibonacci sequence:")
print(f(n))
#Using recursion:
def f(n):
if n == 1 or n == 0 : return n
else:
return f(n-1) + f(n-2)
n = input("Enter a number, bitch:")
print(f(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment