Skip to content

Instantly share code, notes, and snippets.

@zzarcon
Created January 9, 2017 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzarcon/4b8cd1c3b686f81e56c554b96dfc9600 to your computer and use it in GitHub Desktop.
Save zzarcon/4b8cd1c3b686f81e56c554b96dfc9600 to your computer and use it in GitHub Desktop.
int fibonacci(int n) {
int a = 1;
int b = 1;
while (n-- > 1) {
int t = a;
a = b;
b += t;
}
return b;
}
int fibonacciRec(int num) {
if (num <= 1) return 1;
return fibonacciRec(num - 1) + fibonacciRec(num - 2);
}
int memo[10000];
int fibonacciMemo(int n) {
if (memo[n] != -1) return memo[n];
if (n == 1 || n == 2) {
return 1;
} else {
return memo[n] = fibonacciMemo(n - 1) + fibonacciMemo(n - 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment