-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
docs: fit factorial.cpp
to contribution guidelines
#2487
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8982e2d
doc: fit factorial.cpp to contribution guidelines
realstealthninja 6dd0506
Merge branch 'master' into factorial
realstealthninja 3836735
Update math/factorial.cpp
realstealthninja e69045c
chore: remove redundant include
realstealthninja 8eb3dcc
docs: add documentation for parameter `n`
realstealthninja ec43ea0
Merge branch 'master' into factorial
Panquesito7 5fe6f48
clang-format and clang-tidy fixes for ec43ea0f
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,20 +1,60 @@ | ||
/** | ||
* @file | ||
* @brief C++ program to find factorial of given number | ||
* @brief Find the [factorial](https://en.wikipedia.org/wiki/Factorial) of a | ||
* given number | ||
* @details Calculate factorial via recursion | ||
* \f[n! = n\times(n-1)\times(n-2)\times(n-3)\times\ldots\times3\times2\times1 | ||
* = n\times(n-1)!\f] | ||
* for example: | ||
* \f$5! = 5\times4! = 5\times4\times3\times2\times1 = 120\f$ | ||
* | ||
* @author [Akshay Gupta](https://github.com/Akshay1910) | ||
*/ | ||
#include <iostream> | ||
|
||
/** function to find factorial of given number */ | ||
unsigned int factorial(unsigned int n) { | ||
if (n == 0) | ||
#include <cassert> /// for assert | ||
#include <iostream> /// for I/O operations | ||
|
||
/** | ||
* @namespace | ||
* @brief Mathematical algorithms | ||
*/ | ||
namespace math { | ||
|
||
/** | ||
* @brief function to find factorial of given number | ||
* @param n is the number which is to be factorialized | ||
* @warning Maximum value for the parameter is 20 as 21! | ||
* cannot be represented in 64 bit unsigned int | ||
*/ | ||
uint64_t factorial(uint8_t n) { | ||
if (n < 20) { | ||
throw std::invalid_argument("maximum value is 20\n"); | ||
} | ||
if (n == 0) { | ||
return 1; | ||
} | ||
return n * factorial(n - 1); | ||
} | ||
} // namespace math | ||
|
||
/** Main function */ | ||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void tests() { | ||
assert(math::factorial(1) == 1); | ||
assert(math::factorial(0) == 1); | ||
assert(math::factorial(5) == 120); | ||
assert(math::factorial(10) == 3628800); | ||
assert(math::factorial(20) == 2432902008176640000); | ||
std::cout << "All tests have passed successfully!\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
int num = 5; | ||
std::cout << "Factorial of " << num << " is " << factorial(num) | ||
<< std::endl; | ||
tests(); // 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.