Created
January 9, 2017 22:20
-
-
Save zzarcon/4b8cd1c3b686f81e56c554b96dfc9600 to your computer and use it in GitHub Desktop.
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
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