From b0d0c3246d0a6bcf2f844383354b137ba3a9de58 Mon Sep 17 00:00:00 2001 From: ritk20 Date: Tue, 8 Oct 2024 21:09:06 +0530 Subject: [PATCH 1/3] Update fibonacci.cpp --- math/fibonacci.cpp | 73 +++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 4e3c15de86e..2d84ef7f6aa 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -1,68 +1,63 @@ /** * @file - * @brief Generate fibonacci sequence + * @brief Naive recursive algorithm to calculate the n-th [Fibonacci + * number](https://en.wikipedia.org/wiki/Fibonacci_sequence). * + * @details * 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 */ -#include -#include +#include /// for assert +#include /// for IO operations /** - * Recursively compute sequences - * @param n input + * @namespace math + * @brief Math algorithms + */ +namespace math { +/** + * @namespace fibonacci + * @brief Functions for Fibonacci sequence + */ +namespace fibonacci { +/** + * @brief Function to compute the n-th Fibonacci number + * @param n the index of the Fibonacci number * @returns n-th element of the Fbinacci's sequence */ uint64_t fibonacci(uint64_t n) { - /* If the input is 0 or 1 just return the same - This will set the first 2 values of the sequence */ + // If the input is 0 or 1 just return the same + // This will set the first 2 values of the sequence if (n <= 1) { return n; } - /* Add the last 2 values of the sequence to get next */ + // Add the last 2 values of the sequence to get next return fibonacci(n - 1) + fibonacci(n - 2); } +} // namespace fibonacci +} // namespace math /** - * Function for testing the fibonacci() function with a few - * test cases and assert statement. + * @brief Self-test implementation * @returns `void` -*/ + */ static void test() { - uint64_t test_case_1 = fibonacci(0); - assert(test_case_1 == 0); - std::cout << "Passed Test 1!" << std::endl; - - uint64_t test_case_2 = fibonacci(1); - assert(test_case_2 == 1); - std::cout << "Passed Test 2!" << std::endl; - - uint64_t test_case_3 = fibonacci(2); - assert(test_case_3 == 1); - std::cout << "Passed Test 3!" << std::endl; - - uint64_t test_case_4 = fibonacci(3); - assert(test_case_4 == 2); - std::cout << "Passed Test 4!" << std::endl; - - uint64_t test_case_5 = fibonacci(4); - assert(test_case_5 == 3); - std::cout << "Passed Test 5!" << std::endl; - - uint64_t test_case_6 = fibonacci(15); - assert(test_case_6 == 610); - std::cout << "Passed Test 6!" << std::endl << std::endl; + assert(math::fibonacci::fibonacci(0) == 0); + assert(math::fibonacci::fibonacci(1) == 1); + assert(math::fibonacci::fibonacci(2) == 1); + assert(math::fibonacci::fibonacci(3) == 2); + assert(math::fibonacci::fibonacci(4) == 3); + assert(math::fibonacci::fibonacci(15) == 610); + assert(math::fibonacci::fibonacci(20) == 6765); + std::cout << "All tests have passed successfully!\n"; } /// Main function int main() { - test(); - int n = 0; - std::cin >> n; - assert(n >= 0); - std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl; + test(); // run self-test implementations + return 0; } From 78552327ccf501e1fa4f0e77147d83070ff2d838 Mon Sep 17 00:00:00 2001 From: ritk20 Date: Tue, 8 Oct 2024 21:12:01 +0530 Subject: [PATCH 2/3] update documentation --- math/fibonacci.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 2d84ef7f6aa..4be46fc9f37 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -56,7 +56,10 @@ static void test() { std::cout << "All tests have passed successfully!\n"; } -/// Main function +/** + * @brief Main function + * @returns 0 on exit + */ int main() { test(); // run self-test implementations return 0; From 5d8be6f025439667ccd0c0d48536887591ccf299 Mon Sep 17 00:00:00 2001 From: ritk20 Date: Wed, 9 Oct 2024 08:30:17 +0530 Subject: [PATCH 3/3] Update documentation --- math/fibonacci.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 4be46fc9f37..a09ebb57822 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -1,11 +1,10 @@ /** * @file - * @brief Naive recursive algorithm to calculate the n-th [Fibonacci + * @brief n-th [Fibonacci * number](https://en.wikipedia.org/wiki/Fibonacci_sequence). * * @details - * Calculate the the value on Fibonacci's sequence given an - * integer as input. + * Naive recursive implementation to calculate the n-th Fibonacci number. * \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f] * * @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp @@ -26,16 +25,16 @@ namespace fibonacci { /** * @brief Function to compute the n-th Fibonacci number * @param n the index of the Fibonacci number - * @returns n-th element of the Fbinacci's sequence + * @returns n-th element of the Fibonacci's sequence */ uint64_t fibonacci(uint64_t n) { - // If the input is 0 or 1 just return the same + // If the input is 0 or 1 just return the same (Base Case) // This will set the first 2 values of the sequence if (n <= 1) { return n; } - // Add the last 2 values of the sequence to get next + // Add the preceding 2 values of the sequence to get next return fibonacci(n - 1) + fibonacci(n - 2); } } // namespace fibonacci