-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
fix:fit check_factorial.cpp
to guidelines
#2466
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
14 commits
Select commit
Hold shift + click to select a range
493a700
Quality of life update
ewd00010 2de9492
Update math/check_factorial.cpp
ewd00010 b59cdfb
Update math/check_factorial.cpp
ewd00010 4cc6410
Update math/check_factorial.cpp
ewd00010 0953db5
Update math/check_factorial.cpp
ewd00010 b8f62b9
Update math/check_factorial.cpp
ewd00010 c48cb0e
Update math/check_factorial.cpp
ewd00010 ce408a4
Update math/check_factorial.cpp
ewd00010 e26b6a2
Update math/check_factorial.cpp
ewd00010 d7bc8d0
chore: apply suggestions from code review
Panquesito7 7548b13
Updated last return in is_factorial
ewd00010 41a2f3f
Update math/check_factorial.cpp
ewd00010 5f4174e
Merge branch 'master' into check_factorial
realstealthninja 1159b23
Merge branch 'master' into check_factorial
Panquesito7 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,64 +1,73 @@ | ||
/** | ||
* @file | ||
* @brief A simple program to check if the given number is a factorial of some | ||
* @brief A simple program to check if the given number is a [factorial](https://en.wikipedia.org/wiki/Factorial) of some | ||
* number or not. | ||
* | ||
* @details A factorial number is the sum of k! where any value of k is a | ||
* positive integer. https://www.mathsisfun.com/numbers/factorial.html | ||
* | ||
* @author [Divyajyoti Ukirde](https://github.com/divyajyotiuk) | ||
* @author [ewd00010](https://github.com/ewd00010) | ||
*/ | ||
#include <cassert> | ||
#include <iostream> | ||
#include <cassert> /// for assert | ||
#include <iostream> /// for cout | ||
|
||
/** | ||
* Function to check if the given number is factorial of some number or not. | ||
* @namespace | ||
* @brief Mathematical algorithms | ||
*/ | ||
namespace math { | ||
ewd00010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief Function to check if the given number is factorial of some number or | ||
* not. | ||
* @param n number to be checked. | ||
* @return if number is a factorial, returns true, else false. | ||
* @return true if number is a factorial returns true | ||
* @return false if number is not a factorial | ||
*/ | ||
|
||
bool is_factorial(uint64_t n) { | ||
if (n <= 0) { | ||
if (n <= 0) { // factorial numbers are only ever positive Integers | ||
return false; | ||
} | ||
for (uint32_t i = 1;; i++) { | ||
if (n % i != 0) { | ||
break; | ||
} | ||
|
||
/*! | ||
* this loop is basically a reverse factorial calculation, where instead | ||
* of multiplying we are dividing. We start at i = 2 since i = 1 has | ||
* no impact division wise | ||
*/ | ||
int i = 2; | ||
while (n % i == 0) { | ||
n = n / i; | ||
i++; | ||
} | ||
if (n == 1) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
|
||
/*! | ||
* if n was the sum of a factorial then it should be divided until it | ||
* becomes 1 | ||
*/ | ||
return (n == 1); | ||
} | ||
} // namespace math | ||
|
||
/** Test function | ||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
void tests() { | ||
std::cout << "Test 1:\t n=50\n"; | ||
assert(is_factorial(50) == false); | ||
std::cout << "passed\n"; | ||
|
||
std::cout << "Test 2:\t n=720\n"; | ||
assert(is_factorial(720) == true); | ||
std::cout << "passed\n"; | ||
static void tests() { | ||
assert(math::is_factorial(50) == false); | ||
assert(math::is_factorial(720) == true); | ||
assert(math::is_factorial(0) == false); | ||
assert(math::is_factorial(1) == true); | ||
assert(math::is_factorial(479001600) == true); | ||
assert(math::is_factorial(-24) == false); | ||
|
||
std::cout << "Test 3:\t n=0\n"; | ||
assert(is_factorial(0) == false); | ||
std::cout << "passed\n"; | ||
|
||
std::cout << "Test 4:\t n=479001600\n"; | ||
assert(is_factorial(479001600) == true); | ||
std::cout << "passed\n"; | ||
|
||
std::cout << "Test 5:\t n=-24\n"; | ||
assert(is_factorial(-24) == false); | ||
std::cout << "passed\n"; | ||
std::cout << "All tests have successfully passed!" << std::endl; | ||
} | ||
|
||
/** Main function | ||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
tests(); | ||
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.