Skip to content

Instantly share code, notes, and snippets.

@jrm2k6
Created August 28, 2013 21:37
Show Gist options
  • Select an option

  • Save jrm2k6/6371652 to your computer and use it in GitHub Desktop.

Select an option

Save jrm2k6/6371652 to your computer and use it in GitHub Desktop.
Memoized fibonacci
import sys
def fibonacci(n, fib_values):
if n < len(fib_values):
return fib_values[n]
else:
fib_values.append(n if n < 2 else fibonacci(n-2, fib_values) + fibonacci(n-1, fib_values))
return fib_values[n]
def run(i):
fib_values = [0, 1, 1]
fibonacci(i, fib_values)
p = map(str, fib_values)
print '\n'.join(p[0:i])
for i in range(int(sys.stdin.readline())):
run(int(sys.stdin.readline().strip()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment