Last active
August 29, 2015 13:57
-
-
Save burt202/9503516 to your computer and use it in GitHub Desktop.
Fibonacci Functions
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) { | |
var i, fibs = []; | |
fibs.push(0); | |
fibs.push(1); | |
for (i = 0; i < n; i++) { | |
fibs.push(fibs[0] + fibs[1]); | |
fibs.shift(); | |
} | |
return fibs[0]; | |
} | |
for (i = 0; i <= 20; i++) { | |
console.log(i + " = " + fib(i)); | |
} |
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) { | |
if (n === 0) { | |
return 0; | |
} else if (n === 1) { | |
return 1; | |
} else { | |
return fib(n - 1) + fib(n - 2); | |
} | |
} | |
for (i = 0; i <= 20; i++) { | |
console.log(i + " = " + fib(i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment