forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_factorial.cpp
64 lines (56 loc) · 1.33 KB
/
check_factorial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @file
* @brief A simple program to check if the given number is a factorial of some
* number or not.
* @author [Divyajyoti Ukirde](https://github.com/divyajyotiuk)
*/
#include <cassert>
#include <iostream>
/**
* 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.
*/
bool is_factorial(uint64_t n) {
if (n <= 0) {
return false;
}
for (uint32_t i = 1;; i++) {
if (n % i != 0) {
break;
}
n = n / i;
}
if (n == 1) {
return true;
} else {
return false;
}
}
/** Test function
* @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";
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";
}
/** Main function
* @returns 0 on exit
*/
int main() {
tests();
return 0;
}