-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix: Update documentations and tests for fibonacci.cpp #2793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+40
−43
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,66 @@ | ||
/** | ||
* @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 <cassert> | ||
#include <iostream> | ||
#include <cassert> /// for assert | ||
#include <iostream> /// 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 | ||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.