Last active
September 16, 2018 03:26
-
-
Save pixelsnob/585449f5fda0f96004642748ac6efcf5 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 fib(n) { | |
if (n < 1) { | |
return 1; | |
} | |
let a = 1, b = 0; | |
while (n >= 0) { | |
[ a, b ] = [ b, a + b ]; | |
n--; | |
} | |
return b; | |
}; | |
function fibRecursive(n) { | |
if (n <= 1) { | |
return 1; | |
} | |
return fib(n - 1) + fib(n - 2); | |
}; | |
function fibCheck(num){ | |
var a = 1, b = 0, temp; | |
while (num >= 0){ | |
temp = a; | |
a = a + b; | |
b = temp; | |
num--; | |
} | |
return b; | |
} | |
console.log(fib(8), fibCheck(8)); | |
console.log(fib(18), fibCheck(18)); | |
console.log(fib(2), fibCheck(2)); | |
console.log(fib(1), fibCheck(1)); | |
console.log(fibRecursive(8), fibCheck(8)); | |
console.log(fibRecursive(18), fibCheck(18)); | |
console.log(fibRecursive(2), fibCheck(2)); | |
console.log(fibRecursive(1), fibCheck(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment