|
1 | 1 | /**
|
2 | 2 | * @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 |
4 | 4 | * 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 | + * |
5 | 9 | * @author [Divyajyoti Ukirde](https://github.com/divyajyotiuk)
|
| 10 | + * @author [ewd00010](https://github.com/ewd00010) |
6 | 11 | */
|
7 |
| -#include <cassert> |
8 |
| -#include <iostream> |
| 12 | +#include <cassert> /// for assert |
| 13 | +#include <iostream> /// for cout |
9 | 14 |
|
10 | 15 | /**
|
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. |
12 | 23 | * @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 |
14 | 26 | */
|
15 |
| - |
16 | 27 | bool is_factorial(uint64_t n) {
|
17 |
| - if (n <= 0) { |
| 28 | + if (n <= 0) { // factorial numbers are only ever positive Integers |
18 | 29 | return false;
|
19 | 30 | }
|
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) { |
24 | 39 | n = n / i;
|
| 40 | + i++; |
25 | 41 | }
|
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); |
31 | 48 | }
|
| 49 | +} // namespace math |
32 | 50 |
|
33 |
| -/** Test function |
| 51 | +/** |
| 52 | + * @brief Self-test implementations |
34 | 53 | * @returns void
|
35 | 54 | */
|
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); |
44 | 62 |
|
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; |
56 | 64 | }
|
57 | 65 |
|
58 |
| -/** Main function |
| 66 | +/** |
| 67 | + * @brief Main function |
59 | 68 | * @returns 0 on exit
|
60 | 69 | */
|
61 | 70 | int main() {
|
62 |
| - tests(); |
| 71 | + tests(); // run self-test implementations |
63 | 72 | return 0;
|
64 | 73 | }
|
0 commit comments