Skip to content

Instantly share code, notes, and snippets.

@hillal20
Last active September 25, 2018 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillal20/bf56e0e52b930bdc8fd178bd0577166d to your computer and use it in GitHub Desktop.
Save hillal20/bf56e0e52b930bdc8fd178bd0577166d to your computer and use it in GitHub Desktop.
FamousFrizzyEngineering created by hillal20 - https://repl.it/@hillal20/FamousFrizzyEngineering
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