Last active
December 17, 2015 14:38
-
-
Save t-miya/5625426 to your computer and use it in GitHub Desktop.
フィボナッチ数の求め方メモ
fib1の方は二重再帰で効率が悪いので末尾再帰になっているfib2の方が良い
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
def fib1(n): | |
if n<3: | |
return 1 | |
else: | |
return fib1(n-1) + fib1(n-2) | |
def fib2(n, fn1=1, fn=0): | |
if n<1: | |
return fn1 | |
else: | |
return fib2(n-1, fn1+fn, fn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment