Created
May 15, 2013 03:10
-
-
Save haio/5581405 to your computer and use it in GitHub Desktop.
fib algorithm
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 fib (n) { | |
if (n==0 || n==1) { | |
return 1; | |
} else { | |
return fib(n-2) + fib(n-1); | |
} | |
} | |
console.log(fib(3)); | |
function fib (a, b, n) { | |
if (n==0) { | |
return a; | |
} else { | |
return fib(b, a+b, n-1); | |
} | |
} | |
console.log(fib(1,1,3)); | |
function fib (n) { | |
var a=1 | |
, b=1; | |
n -= 1; | |
while (n>0) { | |
var temp = a; | |
a = a+b; | |
b = temp; | |
n -= 1; | |
} | |
return a; | |
} | |
console.log(fib(3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment