Last active
February 28, 2019 02:41
-
-
Save userkang/d2724add61c5c4b808d1f83a1c749602 to your computer and use it in GitHub Desktop.
斐波那契数列
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
function fn(n) { | |
if (n < 1) { | |
return 0 | |
} | |
if (n === 1) { | |
return 1 | |
} | |
if (n === 2) { | |
return 2 | |
} | |
var a = 1 | |
var b = 2 | |
var temp = 0 | |
for (var i = 3; i <= n; i++) { | |
temp = a + b | |
a = b | |
b = temp | |
} | |
return temp | |
} |
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
function fn(n) { | |
if (n < 1) { | |
return 0 | |
} | |
if (n === 1) { | |
return 1 | |
} | |
if (n === 2) { | |
return 2 | |
} | |
return fn(n - 1) + fn(n - 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment