2020-06-20 00:04:56 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Generate fibonacci sequence
|
|
|
|
*
|
|
|
|
* Calculate the the value on Fibonacci's sequence given an
|
|
|
|
* integer as input.
|
|
|
|
* \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
|
|
|
|
*
|
|
|
|
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
|
|
|
|
*/
|
2020-05-19 23:50:15 +08:00
|
|
|
#include <cassert>
|
2020-06-20 00:04:56 +08:00
|
|
|
#include <iostream>
|
2020-05-19 23:50:15 +08:00
|
|
|
|
2020-06-20 00:04:56 +08:00
|
|
|
/**
|
|
|
|
* Recursively compute sequences
|
2020-08-26 07:56:49 +08:00
|
|
|
* @param n input
|
|
|
|
* @returns n-th element of the Fbinacci's sequence
|
2020-06-20 00:04:56 +08:00
|
|
|
*/
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t fibonacci(uint64_t 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-08-26 07:56:49 +08:00
|
|
|
if (n <= 1) {
|
2020-05-19 23:50:15 +08:00
|
|
|
return n;
|
2020-08-26 07:56:49 +08:00
|
|
|
}
|
2020-05-19 23:50:15 +08:00
|
|
|
|
|
|
|
/* Add the last 2 values of the sequence to get next */
|
2020-06-20 00:04:56 +08:00
|
|
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
2020-05-19 23:50:15 +08:00
|
|
|
}
|
|
|
|
|
2020-08-14 19:58:40 +08:00
|
|
|
/**
|
|
|
|
* Function for testing the fibonacci() function with a few
|
|
|
|
* test cases and assert statement.
|
2020-08-15 12:51:06 +08:00
|
|
|
* @returns `void`
|
|
|
|
*/
|
|
|
|
static void test() {
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t test_case_1 = fibonacci(0);
|
2020-08-14 19:58:40 +08:00
|
|
|
assert(test_case_1 == 0);
|
|
|
|
std::cout << "Passed Test 1!" << std::endl;
|
|
|
|
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t test_case_2 = fibonacci(1);
|
2020-08-14 19:58:40 +08:00
|
|
|
assert(test_case_2 == 1);
|
|
|
|
std::cout << "Passed Test 2!" << std::endl;
|
|
|
|
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t test_case_3 = fibonacci(2);
|
2020-08-14 19:58:40 +08:00
|
|
|
assert(test_case_3 == 1);
|
|
|
|
std::cout << "Passed Test 3!" << std::endl;
|
|
|
|
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t test_case_4 = fibonacci(3);
|
2020-08-14 19:58:40 +08:00
|
|
|
assert(test_case_4 == 2);
|
|
|
|
std::cout << "Passed Test 4!" << std::endl;
|
|
|
|
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t test_case_5 = fibonacci(4);
|
2020-08-14 19:58:40 +08:00
|
|
|
assert(test_case_5 == 3);
|
|
|
|
std::cout << "Passed Test 5!" << std::endl;
|
|
|
|
|
2020-08-26 07:56:49 +08:00
|
|
|
uint64_t test_case_6 = fibonacci(15);
|
2020-08-14 19:58:40 +08:00
|
|
|
assert(test_case_6 == 610);
|
|
|
|
std::cout << "Passed Test 6!" << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:04:56 +08:00
|
|
|
/// Main function
|
2020-05-19 23:50:15 +08:00
|
|
|
int main() {
|
2020-08-14 19:58:40 +08:00
|
|
|
test();
|
2020-08-26 07:56:49 +08:00
|
|
|
int n = 0;
|
2020-05-19 23:50:15 +08:00
|
|
|
std::cin >> n;
|
|
|
|
assert(n >= 0);
|
|
|
|
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
|
|
|
|
}
|