-
-
Save ruandao/b5897ffffd3bf9757f02 to your computer and use it in GitHub Desktop.
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
// 迭代 | |
function fib(n) { | |
function fib-iter(i,n_1, n_2, n) { | |
if 0 == n { | |
return 0 | |
} | |
if 1 == n { | |
return 1 | |
} | |
if i == n { | |
return n_1 + n_2 | |
} | |
return fib-iter((i+1), n_1 + n_2, n_1, n) | |
} | |
return fib-iter(0, 1, 0, n) | |
} | |
// 递归 | |
function fib(n) { | |
if 0 == n { | |
return 0 | |
} | |
if 1 == n { | |
return 1 | |
} | |
return fib(n - 1) + fib(n - 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment