Skip to content

Commit b87c713

Browse files
authored
Merge branch 'master' into hollowcrust-patch-1
2 parents 4eb4e54 + 34d3568 commit b87c713

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161

162162
## Greedy Algorithms
163163
* [Boruvkas Minimum Spanning Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp)
164+
* [Digit Separation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/digit_separation.cpp)
164165
* [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/dijkstra.cpp)
165166
* [Gale Shapley](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/gale_shapley.cpp)
166167
* [Huffman](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/huffman.cpp)
@@ -300,6 +301,7 @@
300301
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/iterative_tree_traversals.cpp)
301302
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
302303
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
304+
* [Lfu Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lfu_cache.cpp)
303305
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
304306
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
305307
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)
@@ -369,6 +371,7 @@
369371
* [Gnome Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/gnome_sort.cpp)
370372
* [Heap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/heap_sort.cpp)
371373
* [Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/insertion_sort.cpp)
374+
* [Insertion Sort Recursive](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/insertion_sort_recursive.cpp)
372375
* [Library Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/library_sort.cpp)
373376
* [Merge Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/merge_insertion_sort.cpp)
374377
* [Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/merge_sort.cpp)

math/fibonacci.cpp

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
11
/**
22
* @file
3-
* @brief Generate fibonacci sequence
3+
* @brief n-th [Fibonacci
4+
* number](https://en.wikipedia.org/wiki/Fibonacci_sequence).
45
*
5-
* Calculate the the value on Fibonacci's sequence given an
6-
* integer as input.
6+
* @details
7+
* Naive recursive implementation to calculate the n-th Fibonacci number.
78
* \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
89
*
910
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
1011
*/
11-
#include <cassert>
12-
#include <iostream>
12+
#include <cassert> /// for assert
13+
#include <iostream> /// for IO operations
1314

1415
/**
15-
* Recursively compute sequences
16-
* @param n input
17-
* @returns n-th element of the Fbinacci's sequence
16+
* @namespace math
17+
* @brief Math algorithms
18+
*/
19+
namespace math {
20+
/**
21+
* @namespace fibonacci
22+
* @brief Functions for Fibonacci sequence
23+
*/
24+
namespace fibonacci {
25+
/**
26+
* @brief Function to compute the n-th Fibonacci number
27+
* @param n the index of the Fibonacci number
28+
* @returns n-th element of the Fibonacci's sequence
1829
*/
1930
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 */
31+
// If the input is 0 or 1 just return the same (Base Case)
32+
// This will set the first 2 values of the sequence
2233
if (n <= 1) {
2334
return n;
2435
}
2536

26-
/* Add the last 2 values of the sequence to get next */
37+
// Add the preceding 2 values of the sequence to get next
2738
return fibonacci(n - 1) + fibonacci(n - 2);
2839
}
40+
} // namespace fibonacci
41+
} // namespace math
2942

3043
/**
31-
* Function for testing the fibonacci() function with a few
32-
* test cases and assert statement.
44+
* @brief Self-test implementation
3345
* @returns `void`
34-
*/
46+
*/
3547
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;
48+
assert(math::fibonacci::fibonacci(0) == 0);
49+
assert(math::fibonacci::fibonacci(1) == 1);
50+
assert(math::fibonacci::fibonacci(2) == 1);
51+
assert(math::fibonacci::fibonacci(3) == 2);
52+
assert(math::fibonacci::fibonacci(4) == 3);
53+
assert(math::fibonacci::fibonacci(15) == 610);
54+
assert(math::fibonacci::fibonacci(20) == 6765);
55+
std::cout << "All tests have passed successfully!\n";
5956
}
6057

61-
/// Main function
58+
/**
59+
* @brief Main function
60+
* @returns 0 on exit
61+
*/
6262
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;
63+
test(); // run self-test implementations
64+
return 0;
6865
}

0 commit comments

Comments
 (0)