2020-05-19 23:50:15 +08:00
|
|
|
#include <cassert>
|
2020-05-26 10:13:52 +08:00
|
|
|
#include <iostream>
|
2020-05-19 23:50:15 +08:00
|
|
|
|
|
|
|
/* Calculate the the value on Fibonacci's sequence given an
|
|
|
|
integer as input
|
|
|
|
Fibonacci = 0, 1, 1, 2, 3, 5,
|
|
|
|
8, 13, 21, 34, 55,
|
|
|
|
89, 144, ... */
|
|
|
|
|
2020-05-26 10:13:52 +08:00
|
|
|
int fibonacci(unsigned int n) {
|
2020-05-19 23:50:15 +08:00
|
|
|
/* If the input is 0 or 1 just return the same
|
|
|
|
This will set the first 2 values of the sequence */
|
2020-05-26 10:13:52 +08:00
|
|
|
if (n <= 1) return n;
|
2020-05-19 23:50:15 +08:00
|
|
|
|
|
|
|
/* Add the last 2 values of the sequence to get next */
|
2020-05-26 10:13:52 +08:00
|
|
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
2020-05-19 23:50:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int n;
|
|
|
|
std::cin >> n;
|
|
|
|
assert(n >= 0);
|
|
|
|
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
|
|
|
|
}
|