Skip to content

Instantly share code, notes, and snippets.

@FrenzyExists
Last active November 4, 2023 01:10
Show Gist options
  • Save FrenzyExists/0a649878e66d472a926997822f91ad73 to your computer and use it in GitHub Desktop.
Save FrenzyExists/0a649878e66d472a926997822f91ad73 to your computer and use it in GitHub Desktop.
Recursive Fibbonaci Sequence using Dynamic Programming
#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