Last active
July 19, 2016 05:44
-
-
Save aadimator/cbc9ca0c9f8aa51078e4a43e7f3b9a23 to your computer and use it in GitHub Desktop.
Small Fibonacci Number
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
#include <iostream> | |
int calc_fib(int n) { | |
if (n <= 1) | |
return n; | |
return calc_fib(n - 1) + calc_fib(n - 2); | |
} | |
long long calc_fib_faster (int n) { | |
long long arr [n + 1]; | |
arr[0] = 0; | |
arr[1] = 1; | |
for (int i = 2; i <= n; i++) { | |
arr[i] = arr[i - 1] + arr[i - 2]; | |
} | |
return arr[n]; | |
} | |
int main() { | |
int n = 0; | |
std::cin >> n; | |
// std::cout << calc_fib(n) << std::endl; | |
std::cout << calc_fib_faster(n) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment