## Krishnamurthy Number Checking

Back to Programming

### Description

A number is said to be a Krishnamurthy number if the sum of the factorials of its digits returns the original number.

For example, if the number is 145, then the sum of the factors of the digits can be calculated as:

1! + 4! + 5! = 1 + 24 + 120 = 145 which is equal to the original number. Therefore, 145 is a Krishnamurthy number.

If the number is 123, then the sum of the factors of the digits will be calculated as:

1! + 2! + 3! = 1 + 2 + 6 = 9 which is not equal to the original number. Therefore, 123 is not a Krishnamurthy number.

Here, the number is taken as input then the factorials of each digit are calculated and added together. After that the sum of the factors will be compared with the original number. If both are same then the number is a Krishnamurthy number otherwise not.

### Algorithm

``````INPUT: A number.
OUTPUT: Whether the number is a Krishnamurthy number or not.
PROCESS:
Step 1: [Taking the input]
Read n [the number to be checked]
Step 2: [Checking for Krishnamurthy number]
Set s<-0
Set tmp<-n
[Finding the sum of the factorial of digits of the number]
While n>0 repeat
[Extracting each digit of the number]
Set r<-n mod 10
[Finding the factorial of each digit]
Set f<-1
For i=1 to r repeat
Set f<-f×i
[End of ‘for’ loop]
Set s<-s+f
Set n<-n/10
[End of ‘while’ loop]
[Printing the result according to the condition]
If tmp=s then
Print "The given number is a Krishnamurthy Number"
Else
Print "The given number is not a Krishnamurthy Number"
[End of ‘if-else’]
Step 3: Stop.
``````

## Time Complexity:

while(n>0)  -------------------------------------O(m) [‘m’ is the number of digits]

{     //extracting each digit of the number

r=n%10;

//finding the factorial of each digit

f=1;

for(i=1;i<=r;i++)   -------------------O(r)

f=f*i;

s=s+f;

n=n/10;  }

//printing the result according to the condition

if(tmp==s)     ---------------------------------------O(1)

printf("The given number is a Krishnamurthy Number");

else

printf("The given number is not a Krishnamurthy Number");

The time complexity of this program is O(m*r)  where ‘mis the number of digits of the number and ‘r’ is the value of each digit.

## Space Complexity:

The space complexity of this program is O(1) as it needs a constant memory space to execute the program.