Skip to content

Instantly share code, notes, and snippets.

@Prottoy2938
Last active September 5, 2024 01:34
Show Gist options
  • Save Prottoy2938/dc5e532a25824fbbc249340fee230f2d to your computer and use it in GitHub Desktop.
Save Prottoy2938/dc5e532a25824fbbc249340fee230f2d to your computer and use it in GitHub Desktop.
asdfadf
//get the nth number in fibonacci sequence, base case is 0 and 1.
//solution 1, easier to understand but it's not good. It has a time complexity of O(2^n).
function fib(num) {
if (num === 2) return 1;
if (num === 1) return 0;
return fib(num - 1) + fib(num - 2);
}
fib(6) //returns 5
//solution 2. Better, uses memoization to stop repeating the same task. It has a time complexity of O(n). Much better than the previous solution.
function fib(num, memo = []) {
//edge case if (num === 0) return null;
if (memo[num] !== undefined) return memo[num];
if (num === 2) return 1;
if (num === 1) return 0;
const result = fib(num - 1) + fib(num - 2);
memo[num] = result;
return result;
}
fib(6); //returns 5
.sdfaf po029zdfaf-sdfa1
/asdf pppp0291
ew0291
NA
Nush77777
sd openalbi
asdfwhoami?4
//sadf Bingo210
asdfll29adf Iamprottoy001 bas
https://github.com/Prottoy2938 https://github.com/Prottoy2938/test
@richard523
Copy link

asdfadf my internet now works. Thank you for your recursive fibs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment