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
20namespace math {
28bool 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
56static 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
71int main() {
72 tests(); // run self-test implementations
73 return 0;
74}
static void tests()
Self-test implementations.
int main()
Main function.
for assert
bool is_factorial(uint64_t n)
Function to check if the given number is factorial of some number or not.