TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
check_factorial.cpp
Go to the documentation of this file.
1
12
#include <cassert>
13
#include <cstdint>
14
#include <iostream>
15
20
namespace
math
{
28
bool
is_factorial
(uint64_t n) {
29
if
(n <= 0) {
// factorial numbers are only ever positive Integers
30
return
false
;
31
}
32
38
int
i = 2;
39
while
(n % i == 0) {
40
n = n / i;
41
i++;
42
}
43
48
return
(n == 1);
49
}
50
}
// namespace math
51
56
static
void
tests
() {
57
assert(
math::is_factorial
(50) ==
false
);
58
assert(
math::is_factorial
(720) ==
true
);
59
assert(
math::is_factorial
(0) ==
false
);
60
assert(
math::is_factorial
(1) ==
true
);
61
assert(
math::is_factorial
(479001600) ==
true
);
62
assert(
math::is_factorial
(-24) ==
false
);
63
64
std::cout <<
"All tests have successfully passed!"
<< std::endl;
65
}
66
71
int
main
() {
72
tests
();
// run self-test implementations
73
return
0;
74
}
tests
static void tests()
Self-test implementations.
Definition
check_factorial.cpp:56
main
int main()
Main function.
Definition
check_factorial.cpp:71
math
for assert
math::is_factorial
bool is_factorial(uint64_t n)
Function to check if the given number is factorial of some number or not.
Definition
check_factorial.cpp:28
math
check_factorial.cpp
Generated by
1.12.0