Last active
September 25, 2018 04:18
-
-
Save hillal20/bf56e0e52b930bdc8fd178bd0577166d to your computer and use it in GitHub Desktop.
FamousFrizzyEngineering created by hillal20 - https://repl.it/@hillal20/FamousFrizzyEngineering
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 naivefib(n){ | |
if ( n < 3 ){ | |
return 1 | |
} | |
return naivefib(n-1) + naivefib(n-2) | |
} | |
console.log(naivefib(20)) | |
/// memorized solution | |
let result | |
let arr = [] | |
function fib(n){ | |
if (arr[n] !== null && arr[n]!== undefined){ | |
return arr[n] | |
} | |
if ( n < 3 ){ | |
return 1 | |
} | |
else{ | |
result = fib(n-1) + fib(n-2) | |
arr[n]= result | |
return result | |
} | |
} | |
console.log(fib(1000)) | |
//////// buttom up | |
let newarr = [] | |
function newfib(n){ | |
if ( n < 3 ){ | |
return 1 | |
} | |
newarr[1] = 1; | |
newarr[2] = 1; | |
for(let i = 3; i <= n ; i ++ ){ | |
newarr[i] = newarr[i-1] + newarr[i-2] | |
} | |
return newarr[n] | |
} | |
console.log(newfib(1000)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment