Created
August 28, 2013 21:37
-
-
Save jrm2k6/6371652 to your computer and use it in GitHub Desktop.
Memoized fibonacci
This file contains hidden or 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
| 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