Skip to content

Commit b0d0c32

Browse files
committed
Update fibonacci.cpp
1 parent faff217 commit b0d0c32

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

math/fibonacci.cpp

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,63 @@
11
/**
22
* @file
3-
* @brief Generate fibonacci sequence
3+
* @brief Naive recursive algorithm to calculate the n-th [Fibonacci
4+
* number](https://en.wikipedia.org/wiki/Fibonacci_sequence).
45
*
6+
* @details
57
* Calculate the the value on Fibonacci's sequence given an
68
* integer as input.
79
* \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
810
*
911
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
1012
*/
11-
#include <cassert>
12-
#include <iostream>
13+
#include <cassert> /// for assert
14+
#include <iostream> /// for IO operations
1315

1416
/**
15-
* Recursively compute sequences
16-
* @param n input
17+
* @namespace math
18+
* @brief Math algorithms
19+
*/
20+
namespace math {
21+
/**
22+
* @namespace fibonacci
23+
* @brief Functions for Fibonacci sequence
24+
*/
25+
namespace fibonacci {
26+
/**
27+
* @brief Function to compute the n-th Fibonacci number
28+
* @param n the index of the Fibonacci number
1729
* @returns n-th element of the Fbinacci's sequence
1830
*/
1931
uint64_t fibonacci(uint64_t n) {
20-
/* If the input is 0 or 1 just return the same
21-
This will set the first 2 values of the sequence */
32+
// If the input is 0 or 1 just return the same
33+
// This will set the first 2 values of the sequence
2234
if (n <= 1) {
2335
return n;
2436
}
2537

26-
/* Add the last 2 values of the sequence to get next */
38+
// Add the last 2 values of the sequence to get next
2739
return fibonacci(n - 1) + fibonacci(n - 2);
2840
}
41+
} // namespace fibonacci
42+
} // namespace math
2943

3044
/**
31-
* Function for testing the fibonacci() function with a few
32-
* test cases and assert statement.
45+
* @brief Self-test implementation
3346
* @returns `void`
34-
*/
47+
*/
3548
static void test() {
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;
49+
assert(math::fibonacci::fibonacci(0) == 0);
50+
assert(math::fibonacci::fibonacci(1) == 1);
51+
assert(math::fibonacci::fibonacci(2) == 1);
52+
assert(math::fibonacci::fibonacci(3) == 2);
53+
assert(math::fibonacci::fibonacci(4) == 3);
54+
assert(math::fibonacci::fibonacci(15) == 610);
55+
assert(math::fibonacci::fibonacci(20) == 6765);
56+
std::cout << "All tests have passed successfully!\n";
5957
}
6058

6159
/// Main function
6260
int main() {
63-
test();
64-
int n = 0;
65-
std::cin >> n;
66-
assert(n >= 0);
67-
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
61+
test(); // run self-test implementations
62+
return 0;
6863
}

0 commit comments

Comments
 (0)