Posted on by Kalkicode
Code Number

# Check if a given number is factorial of other

The given problem is to check if a given number is the factorial of another number. In mathematics, the factorial of a non-negative integer 'n' is denoted by 'n!' and is the product of all positive integers less than or equal to 'n'. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.

The task is to determine whether the given number 'num' is equal to the factorial of any positive integer 'i' or not. If there exists an 'i' such that 'num' is equal to 'i!', then the program should output 'num is factorial of i'. Otherwise, it should print 'num is not factorial of any number'.

## Explanation with Suitable Example

Let's take a few examples to understand the problem better. We'll use these examples to validate the given code's output as well.

1. Example: num = 24

• 24 is not a factorial of 1 because 1! = 1, and 24 ≠ 1.
• 24 is not a factorial of 2 because 2! = 2, and 24 ≠ 2.
• 24 is not a factorial of 3 because 3! = 6, and 24 ≠ 6.
• 24 is a factorial of 4 because 4! = 24, and 24 = 24.
2. Example: num = 34

• 34 is not a factorial of any number because no positive integer 'i' exists such that i! = 34.

## Pseudocode

``````FUNCTION is_factorial_of(num)
result = 0
IF num = 1 OR num = 0 THEN
result = 1
ELSE IF num > 1 THEN
temp = num
i = 1
WHILE temp % i = 0 DO
temp = temp / i
i = i + 1
ENDWHILE
IF temp = 1 THEN
result = i - 1
ENDIF
ENDIF
IF result != 0 THEN
PRINT num, "is factorial of", result
ELSE
PRINT num, "is not factorial of any number"
ENDIF
END FUNCTION
``````

## Algorithm Explanation

1. Start the function `is_factorial_of(num)` with the given 'num' as the parameter.
2. Initialize 'result' to 0 to store the possible factorial number 'i'.
3. Check if 'num' is 1 or 0. If so, set 'result' to 1 since both 0! and 1! equal 1.
4. If 'num' is greater than 1, enter the loop.
5. Initialize 'temp' with 'num', and 'i' with 1. The loop is meant to find the value of 'i' for which 'num' is a factorial.
6. In the loop, keep dividing 'temp' by 'i' until 'temp' is not divisible by 'i'.
7. If, after the loop, 'temp' becomes 1, it means 'num' is factorial of (i-1). Set 'result' to (i-1).
8. If 'result' is non-zero, it means 'num' is factorial of some number 'i', and print the result accordingly.
9. If 'result' is still zero, 'num' is not a factorial of any number, and print the result accordingly.

## Code Solution

Here given code implementation process.

``````// C program
// Check if a given number is factorial of other
#include <stdio.h>

//Check whether given number is factorial of any number
void is_factorial_of(int num)
{
int result = 0;

if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
int temp = num;
int i = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = temp / i;
// Get next number
i++;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
printf("\n [%d] is factorial of %d", num, result);
}
else
{
printf("\n [%d] is not factorial of any number", num);
}
}
int main()
{
is_factorial_of(24);
is_factorial_of(2);
is_factorial_of(34);
is_factorial_of(720);
is_factorial_of(120);
is_factorial_of(0);
return 0;
}``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````// Java program
// Check if a given number is factorial of other
class FactorialNo
{
//Check whether given number is factorial of any number
public void is_factorial_of(int num)
{
int result = 0;
if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
int temp = num;
int i = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = temp / i;
// Get next number
i++;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
System.out.print("\n [" + num + "] is factorial of " + result);
}
else
{
System.out.print("\n [" + num + "] is not factorial of any number");
}
}
public static void main(String[] args)
{
FactorialNo obj = new FactorialNo();
// Test Case
obj.is_factorial_of(24);
obj.is_factorial_of(2);
obj.is_factorial_of(34);
obj.is_factorial_of(720);
obj.is_factorial_of(120);
obj.is_factorial_of(0);
}
}``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ program
// Check if a given number is factorial of other

class FactorialNo
{
public:
//Check whether given number is factorial of any number
void is_factorial_of(int num)
{
int result = 0;
if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
int temp = num;
int i = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = temp / i;
// Get next number
i++;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
cout << "\n [" << num << "] is factorial of " << result;
}
else
{
cout << "\n [" << num << "] is not factorial of any number";
}
}
};
int main()
{
FactorialNo obj = FactorialNo();
// Test Case
obj.is_factorial_of(24);
obj.is_factorial_of(2);
obj.is_factorial_of(34);
obj.is_factorial_of(720);
obj.is_factorial_of(120);
obj.is_factorial_of(0);
return 0;
}``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````//Include namespace system
using System;

// C# program
// Check if a given number is factorial of other

class FactorialNo
{
//Check whether given number is factorial of any number
public void is_factorial_of(int num)
{
int result = 0;
if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
int temp = num;
int i = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = temp / i;
// Get next number
i++;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
Console.Write("\n [" + num + "] is factorial of " + result);
}
else
{
Console.Write("\n [" + num + "] is not factorial of any number");
}
}
public static void Main(String[] args)
{
FactorialNo obj = new FactorialNo();
// Test Case
obj.is_factorial_of(24);
obj.is_factorial_of(2);
obj.is_factorial_of(34);
obj.is_factorial_of(720);
obj.is_factorial_of(120);
obj.is_factorial_of(0);
}
}``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````<?php
// Php program
// Check if a given number is factorial of other

class FactorialNo
{
//Check whether given number is factorial of any number
public	function is_factorial_of(\$num)
{
\$result = 0;
if (\$num == 1 || \$num == 0)
{
\$result = 1;
}
else if (\$num > 1)
{
//Define loop controlling variable
\$temp = \$num;
\$i = 1;
// Execute loop, until number is divisible by [i]
while (\$temp % \$i == 0)
{
\$temp = intval(\$temp / \$i);
// Get next number
\$i++;
}
if (\$temp == 1)
{
//When remainder of (i-1) is one
\$result = \$i - 1;
}
}
if (\$result != 0)
{
echo "\n [". \$num ."] is factorial of ". \$result;
}
else
{
echo "\n [". \$num ."] is not factorial of any number";
}
}
}

function main()
{
\$obj = new FactorialNo();
// Test Case
\$obj->is_factorial_of(24);
\$obj->is_factorial_of(2);
\$obj->is_factorial_of(34);
\$obj->is_factorial_of(720);
\$obj->is_factorial_of(120);
\$obj->is_factorial_of(0);
}
main();``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````// Node Js program
// Check if a given number is factorial of other
class FactorialNo
{
//Check whether given number is factorial of any number
is_factorial_of(num)
{
var result = 0;
if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
var temp = num;
var i = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = parseInt(temp / i);
// Get next number
i++;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
process.stdout.write("\n [" + num + "] is factorial of " + result);
}
else
{
process.stdout.write("\n [" + num + "] is not factorial of any number");
}
}
}

function main()
{
var obj = new FactorialNo();
// Test Case
obj.is_factorial_of(24);
obj.is_factorial_of(2);
obj.is_factorial_of(34);
obj.is_factorial_of(720);
obj.is_factorial_of(120);
obj.is_factorial_of(0);
}
main();``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````#  Python 3 program
#  Check if a given number is factorial of other
class FactorialNo :
# Check whether given number is factorial of any number
def is_factorial_of(self, num) :
result = 0
if (num == 1 or num == 0) :
result = 1

elif(num > 1) :
# Define loop controlling variable
temp = num
i = 1
#  Execute loop, until number is divisible by [i]
while (temp % i == 0) :
temp = int(temp / i)
#  Get next number
i += 1

if (temp == 1) :
# When remainder of (i-1) is one
result = i - 1

if (result != 0) :
print("\n [", num ,"] is factorial of ", result, end = "")
else :
print("\n [", num ,"] is not factorial of any number", end = "")

def main() :
obj = FactorialNo()
#  Test Case
obj.is_factorial_of(24)
obj.is_factorial_of(2)
obj.is_factorial_of(34)
obj.is_factorial_of(720)
obj.is_factorial_of(120)
obj.is_factorial_of(0)

if __name__ == "__main__": main()``````

#### Output

`````` [ 24 ] is factorial of  4
[ 2 ] is factorial of  2
[ 34 ] is not factorial of any number
[ 720 ] is factorial of  6
[ 120 ] is factorial of  5
[ 0 ] is factorial of  1``````
``````#  Ruby program
#  Check if a given number is factorial of other
class FactorialNo
# Check whether given number is factorial of any number
def is_factorial_of(num)
result = 0
if (num == 1 || num == 0)
result = 1
elsif(num > 1)
# Define loop controlling variable
temp = num
i = 1
#  Execute loop, until number is divisible by [i]
while (temp % i == 0)
temp = temp / i
#  Get next number
i += 1
end

if (temp == 1)
# When remainder of (i-1) is one
result = i - 1
end

end

if (result != 0)
print("\n [", num ,"] is factorial of ", result)
else
print("\n [", num ,"] is not factorial of any number")
end

end

end

def main()
obj = FactorialNo.new()
#  Test Case
obj.is_factorial_of(24)
obj.is_factorial_of(2)
obj.is_factorial_of(34)
obj.is_factorial_of(720)
obj.is_factorial_of(120)
obj.is_factorial_of(0)
end

main()``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````// Scala program
// Check if a given number is factorial of other
class FactorialNo
{
//Check whether given number is factorial of any number
def is_factorial_of(num: Int): Unit = {
var result: Int = 0;
if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
var temp: Int = num;
var i: Int = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = (temp / i).toInt;
// Get next number
i += 1;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
print("\n [" + num + "] is factorial of " + result);
}
else
{
print("\n [" + num + "] is not factorial of any number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: FactorialNo = new FactorialNo();
// Test Case
obj.is_factorial_of(24);
obj.is_factorial_of(2);
obj.is_factorial_of(34);
obj.is_factorial_of(720);
obj.is_factorial_of(120);
obj.is_factorial_of(0);
}
}``````

#### Output

`````` [24] is factorial of 4
[2] is factorial of 2
[34] is not factorial of any number
[720] is factorial of 6
[120] is factorial of 5
[0] is factorial of 1``````
``````// Swift 4 program
// Check if a given number is factorial of other
class FactorialNo
{
//Check whether given number is factorial of any number
func is_factorial_of(_ num: Int)
{
var result: Int = 0;
if (num == 1 || num == 0)
{
result = 1;
}
else if (num > 1)
{
//Define loop controlling variable
var temp: Int = num;
var i: Int = 1;
// Execute loop, until number is divisible by [i]
while (temp % i == 0)
{
temp = temp / i;
// Get next number
i += 1;
}
if (temp == 1)
{
//When remainder of (i-1) is one
result = i - 1;
}
}
if (result != 0)
{
print("\n [\(num)] is factorial of ", result, terminator: "");
}
else
{
print("\n [\(num)] is not factorial of any number", terminator: "");
}
}
}
func main()
{
let obj: FactorialNo = FactorialNo();
// Test Case
obj.is_factorial_of(24);
obj.is_factorial_of(2);
obj.is_factorial_of(34);
obj.is_factorial_of(720);
obj.is_factorial_of(120);
obj.is_factorial_of(0);
}
main();``````

#### Output

`````` [24] is factorial of  4
[2] is factorial of  2
[34] is not factorial of any number
[720] is factorial of  6
[120] is factorial of  5
[0] is factorial of  1``````

## Resultant Output Explanation

The given code is designed to check if the input numbers are factorial of any positive integer 'i' or not. Let's analyze the output for the given input numbers:

1. 24: The code correctly identifies that 24 is the factorial of 4, as 4! = 24.
2. 2: The code correctly identifies that 2 is the factorial of 2, as 2! = 2.
3. 34: The code correctly identifies that 34 is not the factorial of any positive integer.
4. 720: The code correctly identifies that 720 is the factorial of 6, as 6! = 720.
5. 120: The code correctly identifies that 120 is the factorial of 5, as 5! = 120.
6. 0: The code correctly identifies that 0 is the factorial of 1, as 0! = 1.

## Time Complexity of the Code

The time complexity of the code is mainly determined by the while loop. In the worst case scenario, the loop will run until 'temp' becomes 1. The value of 'temp' will decrease by a factor of 'i' in each iteration. Hence, the number of iterations will be approximately log(num) to the base 'i'. So, the overall time complexity can be approximated as O(log(num)).

It is important to note that the code could be further optimized, as it currently checks for factorials up to 'num' itself. However, since the factorial of any number 'n' is already a very large number, it is highly unlikely to have a factorial of a number greater than 'num'. Therefore, the given code still provides a correct solution to the problem.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post