Skip to content

Instantly share code, notes, and snippets.

@lidangzzz
Created November 18, 2020 02:19
Show Gist options
  • Save lidangzzz/1fa7e9fdea20f8c2e47272410cb7e6ff to your computer and use it in GitHub Desktop.
Save lidangzzz/1fa7e9fdea20f8c2e47272410cb7e6ff to your computer and use it in GitHub Desktop.
/*
Function fibonacci
Author: lidangzzz
Input: x: number
Output: fibonacci of x
*/
function fibonacci(x){
if (x<0) return 0;
if (x==1 || x==0) return 1;
//elst x>=2
let dp = [1,1]
for (let i=2;i<=x;i++){ let val = dp[dp.length-1] + dp[dp.length-2]; dp.push(val)}
return dp[x];
}
for(let i=0;i<10;i++) print(fibonacci(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment