Created
March 8, 2015 22:46
-
-
Save lucian303/5573d1f7d407b15e838a to your computer and use it in GitHub Desktop.
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
// Fibonacci | |
// Loop | |
var first = 0, | |
second = 1, | |
total = 0; | |
for (var x = 1; x <= 8; x++) { | |
first = second; | |
second = total; | |
total = first + second; | |
} | |
alert(total); | |
// Recursive | |
function fib(n) { | |
if (n < 2) { | |
return n; | |
} | |
return fib(n - 1) + fib(n - 2); | |
} | |
alert(fib(8)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment