Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
check_factorial.cpp File Reference

A simple program to check if the given number is a factorial of some number or not. More...

#include <cassert>
#include <iostream>
Include dependency graph for check_factorial.cpp:

Functions

bool is_factorial (uint64_t n)
 
void tests ()
 
int main ()
 

Detailed Description

A simple program to check if the given number is a factorial of some number or not.

Author
Divyajyoti Ukirde

Function Documentation

◆ is_factorial()

bool is_factorial ( uint64_t  n)

Function to check if the given number is factorial of some number or not.

Parameters
nnumber to be checked.
Returns
if number is a factorial, returns true, else false.
16  {
17  if (n <= 0) {
18  return false;
19  }
20  for (uint32_t i = 1;; i++) {
21  if (n % i != 0) {
22  break;
23  }
24  n = n / i;
25  }
26  if (n == 1) {
27  return true;
28  } else {
29  return false;
30  }
31 }

◆ main()

int main ( void  )

Main function

Returns
0 on exit
61  {
62  tests();
63  return 0;
64 }
Here is the call graph for this function:

◆ tests()

void tests ( )

Test function

Returns
void
36  {
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";
44 
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";
56 }
Here is the call graph for this function:
std::cout
is_factorial
bool is_factorial(uint64_t n)
Definition: check_factorial.cpp:16
tests
void tests()
Definition: check_factorial.cpp:36