Last active
September 5, 2024 01:34
-
-
Save Prottoy2938/dc5e532a25824fbbc249340fee230f2d to your computer and use it in GitHub Desktop.
asdfadf
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
//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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
asdfadf my internet now works. Thank you for your recursive fibs