Skip to content

Commit 5704841

Browse files
ewd00010realstealthninjaPanquesito7
authored
[feat/docs/fix]: fit check_factorial.cpp to guidelines (#2466)
* Quality of life update FIX: added namespace math, changed for a weird for loop to a while loop with a better conditional and I initiation CHORE: Cleaned documentation and added details plus links. removed the invalid cout lines in test for one line. * Update math/check_factorial.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_factorial.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_factorial.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_factorial.cpp Co-authored-by: David Leal <[email protected]> * Update math/check_factorial.cpp Co-authored-by: David Leal <[email protected]> * Update math/check_factorial.cpp Co-authored-by: David Leal <[email protected]> * Update math/check_factorial.cpp Co-authored-by: David Leal <[email protected]> * Update math/check_factorial.cpp Co-authored-by: David Leal <[email protected]> * chore: apply suggestions from code review * Updated last return in is_factorial * Update math/check_factorial.cpp Co-authored-by: David Leal <[email protected]> --------- Co-authored-by: realstealthninja <[email protected]> Co-authored-by: David Leal <[email protected]>
1 parent ff80be4 commit 5704841

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

math/check_factorial.cpp

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,73 @@
11
/**
22
* @file
3-
* @brief A simple program to check if the given number is a factorial of some
3+
* @brief A simple program to check if the given number is a [factorial](https://en.wikipedia.org/wiki/Factorial) of some
44
* number or not.
5+
*
6+
* @details A factorial number is the sum of k! where any value of k is a
7+
* positive integer. https://www.mathsisfun.com/numbers/factorial.html
8+
*
59
* @author [Divyajyoti Ukirde](https://github.com/divyajyotiuk)
10+
* @author [ewd00010](https://github.com/ewd00010)
611
*/
7-
#include <cassert>
8-
#include <iostream>
12+
#include <cassert> /// for assert
13+
#include <iostream> /// for cout
914

1015
/**
11-
* Function to check if the given number is factorial of some number or not.
16+
* @namespace
17+
* @brief Mathematical algorithms
18+
*/
19+
namespace math {
20+
/**
21+
* @brief Function to check if the given number is factorial of some number or
22+
* not.
1223
* @param n number to be checked.
13-
* @return if number is a factorial, returns true, else false.
24+
* @return true if number is a factorial returns true
25+
* @return false if number is not a factorial
1426
*/
15-
1627
bool is_factorial(uint64_t n) {
17-
if (n <= 0) {
28+
if (n <= 0) { // factorial numbers are only ever positive Integers
1829
return false;
1930
}
20-
for (uint32_t i = 1;; i++) {
21-
if (n % i != 0) {
22-
break;
23-
}
31+
32+
/*!
33+
* this loop is basically a reverse factorial calculation, where instead
34+
* of multiplying we are dividing. We start at i = 2 since i = 1 has
35+
* no impact division wise
36+
*/
37+
int i = 2;
38+
while (n % i == 0) {
2439
n = n / i;
40+
i++;
2541
}
26-
if (n == 1) {
27-
return true;
28-
} else {
29-
return false;
30-
}
42+
43+
/*!
44+
* if n was the sum of a factorial then it should be divided until it
45+
* becomes 1
46+
*/
47+
return (n == 1);
3148
}
49+
} // namespace math
3250

33-
/** Test function
51+
/**
52+
* @brief Self-test implementations
3453
* @returns void
3554
*/
36-
void tests() {
37-
std::cout << "Test 1:\t n=50\n";
38-
assert(is_factorial(50) == false);
39-
std::cout << "passed\n";
40-
41-
std::cout << "Test 2:\t n=720\n";
42-
assert(is_factorial(720) == true);
43-
std::cout << "passed\n";
55+
static void tests() {
56+
assert(math::is_factorial(50) == false);
57+
assert(math::is_factorial(720) == true);
58+
assert(math::is_factorial(0) == false);
59+
assert(math::is_factorial(1) == true);
60+
assert(math::is_factorial(479001600) == true);
61+
assert(math::is_factorial(-24) == false);
4462

45-
std::cout << "Test 3:\t n=0\n";
46-
assert(is_factorial(0) == false);
47-
std::cout << "passed\n";
48-
49-
std::cout << "Test 4:\t n=479001600\n";
50-
assert(is_factorial(479001600) == true);
51-
std::cout << "passed\n";
52-
53-
std::cout << "Test 5:\t n=-24\n";
54-
assert(is_factorial(-24) == false);
55-
std::cout << "passed\n";
63+
std::cout << "All tests have successfully passed!" << std::endl;
5664
}
5765

58-
/** Main function
66+
/**
67+
* @brief Main function
5968
* @returns 0 on exit
6069
*/
6170
int main() {
62-
tests();
71+
tests(); // run self-test implementations
6372
return 0;
6473
}

0 commit comments

Comments
 (0)