Last active
November 4, 2023 01:10
-
-
Save FrenzyExists/0a649878e66d472a926997822f91ad73 to your computer and use it in GitHub Desktop.
Recursive Fibbonaci Sequence using Dynamic Programming
This file contains 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
#include <iostream> | |
#include <map> | |
std::map<int, int> dynoFib; | |
int fibRecursive(int n) { | |
if (n <= 2) { | |
return 1; | |
} else { | |
if(!dynoFib.count(n)) { | |
dynoFib[n] = fibRecursive(n - 1) + fibRecursive(n - 2); | |
} | |
return dynoFib[n]; | |
} | |
} | |
int fib(int n) { | |
dynoFib = {}; | |
int rec = fibRecursive(n); | |
return rec; | |
} | |
int main() { | |
// Write C++ code here | |
int a = fib(15); | |
std::cout << a; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment