|
1 | 1 | /**
|
2 | 2 | * @file
|
3 |
| - * @brief n-th [Fibonacci |
4 |
| - * number](https://en.wikipedia.org/wiki/Fibonacci_sequence). |
| 3 | + * @brief Generate fibonacci sequence |
5 | 4 | *
|
6 |
| - * @details |
7 |
| - * Naive recursive implementation to calculate the n-th Fibonacci number. |
| 5 | + * Calculate the the value on Fibonacci's sequence given an |
| 6 | + * integer as input. |
8 | 7 | * \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
|
9 | 8 | *
|
10 | 9 | * @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
|
11 | 10 | */
|
12 |
| - |
13 |
| -#include <cstdint> /// for std::uint64_t |
14 |
| -#include <cassert> /// for assert |
15 |
| -#include <iostream> /// for IO operations |
16 |
| - |
17 |
| -/** |
18 |
| - * @namespace math |
19 |
| - * @brief Math algorithms |
20 |
| - */ |
21 |
| -namespace math { |
| 11 | +#include <cassert> |
| 12 | +#include <cstdint> |
| 13 | +#include <iostream> |
22 | 14 | /**
|
23 |
| - * @namespace fibonacci |
24 |
| - * @brief Functions for Fibonacci sequence |
25 |
| - */ |
26 |
| -namespace fibonacci { |
27 |
| -/** |
28 |
| - * @brief Function to compute the n-th Fibonacci number |
29 |
| - * @param n the index of the Fibonacci number |
30 |
| - * @returns n-th element of the Fibonacci's sequence |
| 15 | + * Recursively compute sequences |
| 16 | + * @param n input |
| 17 | + * @returns n-th element of the Fbinacci's sequence |
31 | 18 | */
|
32 | 19 | uint64_t fibonacci(uint64_t n) {
|
33 |
| - // If the input is 0 or 1 just return the same (Base Case) |
34 |
| - // This will set the first 2 values of the sequence |
| 20 | + /* If the input is 0 or 1 just return the same |
| 21 | + This will set the first 2 values of the sequence */ |
35 | 22 | if (n <= 1) {
|
36 | 23 | return n;
|
37 | 24 | }
|
38 | 25 |
|
39 |
| - // Add the preceding 2 values of the sequence to get next |
| 26 | + /* Add the last 2 values of the sequence to get next */ |
40 | 27 | return fibonacci(n - 1) + fibonacci(n - 2);
|
41 | 28 | }
|
42 |
| -} // namespace fibonacci |
43 |
| -} // namespace math |
44 | 29 |
|
45 | 30 | /**
|
46 |
| - * @brief Self-test implementation |
| 31 | + * Function for testing the fibonacci() function with a few |
| 32 | + * test cases and assert statement. |
47 | 33 | * @returns `void`
|
48 | 34 | */
|
49 | 35 | static void test() {
|
50 |
| - assert(math::fibonacci::fibonacci(0) == 0); |
51 |
| - assert(math::fibonacci::fibonacci(1) == 1); |
52 |
| - assert(math::fibonacci::fibonacci(2) == 1); |
53 |
| - assert(math::fibonacci::fibonacci(3) == 2); |
54 |
| - assert(math::fibonacci::fibonacci(4) == 3); |
55 |
| - assert(math::fibonacci::fibonacci(15) == 610); |
56 |
| - assert(math::fibonacci::fibonacci(20) == 6765); |
57 |
| - std::cout << "All tests have passed successfully!\n"; |
| 36 | + uint64_t test_case_1 = fibonacci(0); |
| 37 | + assert(test_case_1 == 0); |
| 38 | + std::cout << "Passed Test 1!" << std::endl; |
| 39 | + |
| 40 | + uint64_t test_case_2 = fibonacci(1); |
| 41 | + assert(test_case_2 == 1); |
| 42 | + std::cout << "Passed Test 2!" << std::endl; |
| 43 | + |
| 44 | + uint64_t test_case_3 = fibonacci(2); |
| 45 | + assert(test_case_3 == 1); |
| 46 | + std::cout << "Passed Test 3!" << std::endl; |
| 47 | + |
| 48 | + uint64_t test_case_4 = fibonacci(3); |
| 49 | + assert(test_case_4 == 2); |
| 50 | + std::cout << "Passed Test 4!" << std::endl; |
| 51 | + |
| 52 | + uint64_t test_case_5 = fibonacci(4); |
| 53 | + assert(test_case_5 == 3); |
| 54 | + std::cout << "Passed Test 5!" << std::endl; |
| 55 | + |
| 56 | + uint64_t test_case_6 = fibonacci(15); |
| 57 | + assert(test_case_6 == 610); |
| 58 | + std::cout << "Passed Test 6!" << std::endl << std::endl; |
58 | 59 | }
|
59 | 60 |
|
60 |
| -/** |
61 |
| - * @brief Main function |
62 |
| - * @returns 0 on exit |
63 |
| - */ |
| 61 | +/// Main function |
64 | 62 | int main() {
|
65 |
| - test(); // run self-test implementations |
66 |
| - return 0; |
| 63 | + test(); |
| 64 | + int n = 0; |
| 65 | + std::cin >> n; |
| 66 | + assert(n >= 0); |
| 67 | + std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl; |
67 | 68 | }
|
0 commit comments