Created
July 18, 2020 22:07
-
-
Save ErickGiffoni/d037c3225a7c1d25ea463a1f62873eb9 to your computer and use it in GitHub Desktop.
Given a number *n* as input, the *fib_list()* algorithm is fast and good enough to print out the *n*th number of Fibonacci's sequence.
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
/* | |
Fibonacci number | |
Code by Erick Giffoni - University of Brasilia, Brazil (UnB) | |
*/ | |
#include <iostream> | |
#include <vector> | |
int fib_naive(int n){ | |
if (n <=1) return n; | |
else return fib_naive(n-1) + fib_naive(n-2); | |
} | |
int fib_list(int n){ | |
if(n <= 1) return n; | |
std::vector <int> fib(n+1); | |
fib[0] = 0; | |
fib[1] = 1; | |
for (int i=2; i<=n; i++){ | |
fib[i] = fib[i-1] + fib[i-2]; | |
}//end for | |
return fib[n]; | |
} | |
int main(){ | |
int n; | |
std::cin >> n; | |
std::cout << fib_list(n) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment