Posted on by Kalkicode
Code Number

# Krishnamurthy number program

Krishnamurthy numbers, also known as Strong Numbers, are special types of numbers in mathematics. A number is said to be a Krishnamurthy number if the sum of the factorial of its individual digits is equal to the number itself. For example, let's take the number 145:

1! + 4! + 5! = 1 + 24 + 120 = 145

In this case, 145 is a Krishnamurthy number because the sum of the factorials of its digits is equal to the number itself.

The problem statement can be summarized as follows: Given a positive integer, we need to determine if it is a Krishnamurthy number or not.

## Explanation with Suitable Example

Let's understand the problem statement with the example of the number 145.

1. Extract the individual digits of the number: 1, 4, and 5.
2. Calculate the factorial of each digit: 1! = 1, 4! = 24, and 5! = 120.
3. Sum the factorials: 1 + 24 + 120 = 145.
4. Check if the sum obtained is equal to the original number (145 in this case).
5. Since the sum (145) is equal to the original number (145), we can conclude that 145 is a Krishnamurthy number.

## Standard Pseudocode

``````Function isKrishnamurthyNo(number):
If number < 1:
Return
Initialize i = 1, remainder = 0, sum = 0
Initialize an array factorial[10] to store factorials of digits
factorial[0] = 1
For i = 1 to 9:
factorial[i] = factorial[i-1] * i
Set i = number
While i != 0:
remainder = i % 10
sum += factorial[remainder]
i /= 10
If sum == number:
Print "number is a Krishnamurthy number"
Else:
Print "number is not a Krishnamurthy number"
``````

## Algorithm with Proper Explanation

1. The function `isKrishnamurthyNo` takes an integer `number` as input and checks if it is a Krishnamurthy number or not.

2. The function begins by checking if the input `number` is less than 1. If so, it immediately returns, as negative or zero values cannot be Krishnamurthy numbers.

3. It then initializes some variables: `i` (for iteration), `remainder` (to store the last digit of `i`), and `sum` (to store the sum of factorials of digits).

4. The function also initializes an array `factorial[10]` to store the factorials of digits from 0 to 9. `factorial[0]` is set to 1 since 0! is 1.

5. Using a loop, the function calculates and stores the factorials of digits from 1 to 9 in the `factorial` array.

6. The variable `i` is set to the value of the input `number`, and now the function proceeds to calculate the sum of the factorials of its digits.

7. The while loop runs until `i` becomes 0. In each iteration, it extracts the last digit of `i` using the modulo operation and finds its factorial from the `factorial` array. The factorial is added to the `sum`, and the last digit is removed from `i` by dividing it by 10.

8. After the while loop, the function checks if the `sum` is equal to the original `number`. If they are equal, it means the number is a Krishnamurthy number and is printed as such. Otherwise, the number is not a Krishnamurthy number, and this is also printed.

## Code Solution

Here given code implementation process.

``````// C program
// krishnamurthy number
#include <stdio.h>

// Check whether given number is krishnamurthy number or not
void isKrishnamurthyNo(int number)
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
int i = 1;
int remainder = 0;
int sum = 0;
// Auxiliary space to store factorial
int factorial[10];
// First factorial
factorial[0] = 1;
// Calculate the factorial of range (1..9)
for (i = 1; i <= 9; ++i)
{
factorial[i] = factorial[i - 1] *i;
}
i = number;
// Calculate digit factorial sum of given number
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += factorial[remainder];
// Remove last digit
i /= 10;
}
if (sum == number)
{
printf(" %d is krishnamurthy number \n", number);
}
else
{
printf(" %d is not krishnamurthy number \n", number);
}
}
int main()
{
// Test Case
isKrishnamurthyNo(40585);
isKrishnamurthyNo(65);
isKrishnamurthyNo(145);
return 0;
}``````

#### input

`````` 40585 is krishnamurthy number
65 is not krishnamurthy number
145 is krishnamurthy number``````
``````/*
Java Program for
Krishnamurthy number
*/
class FlaviusNumber
{
// Auxiliary space to store factorial
int[] factorial;
public FlaviusNumber()
{
factorial = new int[10];
this.findFactorial();
}
public void findFactorial()
{
int i = 0;
// First factorial
factorial[0] = 1;
// Calculate the factorial of range (1..9)
for (i = 1; i <= 9; ++i)
{
factorial[i] = factorial[i - 1] * i;
}
}
// Check whether given number is krishnamurthy number or not
public void isKrishnamurthyNo(int number)
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
int i = number;
int remainder = 0;
int sum = 0;
// Calculate digit factorial sum of given number
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += factorial[remainder];
// Remove last digit
i /= 10;
}
if (sum == number)
{
// When result is krishnamurthy number
System.out.println(" [" + number + "] is krishnamurthy number");
}
else
{
System.out.println(" [" + number + "] is not krishnamurthy number");
}
}
public static void main(String[] args)
{
// Test case
}
}``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Krishnamurthy number
*/
class FlaviusNumber
{
public:
// Auxiliary space to store factorial
int *factorial;
FlaviusNumber()
{
this->factorial = new int[10];
this->findFactorial();
}
void findFactorial()
{
int i = 0;
// First factorial
this->factorial[0] = 1;
// Calculate the factorial of range (1..9)
for (i = 1; i <= 9; ++i)
{
this->factorial[i] = this->factorial[i - 1] * i;
}
}
// Check whether given number is krishnamurthy number or not
void isKrishnamurthyNo(int number)
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
int i = number;
int remainder = 0;
int sum = 0;
// Calculate digit factorial sum of given number
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += this->factorial[remainder];
// Remove last digit
i /= 10;
}
if (sum == number)
{
// When result is krishnamurthy number
cout << " [" << number << "] is krishnamurthy number" << endl;
}
else
{
cout << " [" << number << "] is not krishnamurthy number" << endl;
}
}
};
int main()
{
// Test case
return 0;
}``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````
``````// Include namespace system
using System;
/*
Csharp Program for
Krishnamurthy number
*/
public class FlaviusNumber
{
// Auxiliary space to store factorial
int[] factorial;
public FlaviusNumber()
{
this.factorial = new int[10];
this.findFactorial();
}
public void findFactorial()
{
int i = 0;
// First factorial
this.factorial[0] = 1;
// Calculate the factorial of range (1..9)
for (i = 1; i <= 9; ++i)
{
this.factorial[i] = this.factorial[i - 1] * i;
}
}
// Check whether given number is krishnamurthy number or not
public void isKrishnamurthyNo(int number)
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
int i = number;
int remainder = 0;
int sum = 0;
// Calculate digit factorial sum of given number
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += this.factorial[remainder];
// Remove last digit
i /= 10;
}
if (sum == number)
{
// When result is krishnamurthy number
Console.WriteLine(" [" + number + "] is krishnamurthy number");
}
else
{
Console.WriteLine(" [" + number + "] is not krishnamurthy number");
}
}
public static void Main(String[] args)
{
// Test case
}
}``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````
``````<?php
/*
Php Program for
Krishnamurthy number
*/
class FlaviusNumber
{
// Auxiliary space to store factorial
public \$factorial;
public function __construct()
{
\$this->factorial = array_fill(0, 10, 0);
\$this->findFactorial();
}
public function findFactorial()
{
\$i = 0;
// First factorial
\$this->factorial[0] = 1;
// Calculate the factorial of range (1..9)
for (\$i = 1; \$i <= 9; ++\$i)
{
\$this->factorial[\$i] = \$this->factorial[\$i - 1] * \$i;
}
}
// Check whether given number is krishnamurthy number or not
public	function isKrishnamurthyNo(\$number)
{
if (\$number < 1)
{
return;
}
// Define some auxiliary variable
\$i = \$number;
\$remainder = 0;
\$sum = 0;
// Calculate digit factorial sum of given number
while (\$i != 0)
{
// Get the last digit
\$remainder = \$i % 10;
// Factorial sum
\$sum += \$this->factorial[\$remainder];
// Remove last digit
\$i = (int)(\$i / 10);
}
if (\$sum == \$number)
{
// When result is krishnamurthy number
echo " [".\$number."] is krishnamurthy number"."\n";
}
else
{
echo " [".\$number."] is not krishnamurthy number"."\n";
}
}
}

function main()
{
// Test case
}
main();``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````
``````/*
Node JS Program for
Krishnamurthy number
*/
class FlaviusNumber
{
constructor()
{
this.factorial = Array(10).fill(0);
this.findFactorial();
}
findFactorial()
{
var i = 0;
// First factorial
this.factorial[0] = 1;
// Calculate the factorial of range (1..9)
for (i = 1; i <= 9; ++i)
{
this.factorial[i] = this.factorial[i - 1] * i;
}
}
// Check whether given number is krishnamurthy number or not
isKrishnamurthyNo(number)
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
var i = number;
var remainder = 0;
var sum = 0;
// Calculate digit factorial sum of given number
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += this.factorial[remainder];
// Remove last digit
i = parseInt(i / 10);
}
if (sum == number)
{
// When result is krishnamurthy number
console.log(" [" + number + "] is krishnamurthy number");
}
else
{
console.log(" [" + number + "] is not krishnamurthy number");
}
}
}

function main()
{
// Test case
}
main();``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````
``````#  Python 3 Program for
#  Krishnamurthy number
class FlaviusNumber :

def __init__(self) :
self.factorial = [0] * (10)
self.findFactorial()

def findFactorial(self) :
i = 1
#  First factorial
self.factorial[0] = 1
#  Calculate the factorial of range (1..9)
while (i <= 9) :
self.factorial[i] = self.factorial[i - 1] * i
i += 1

#  Check whether given number is krishnamurthy number or not
def isKrishnamurthyNo(self, number) :
if (number < 1) :
return

i = number
remainder = 0
sum = 0
#  Calculate digit factorial sum of given number
while (i != 0) :
#  Get the last digit
remainder = i % 10
#  Factorial sum
sum += self.factorial[remainder]
#  Remove last digit
i = int(i / 10)

if (sum == number) :
#  When result is krishnamurthy number
print(" [", number ,"] is krishnamurthy number")
else :
print(" [", number ,"] is not krishnamurthy number")

def main() :
#  Test case

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

#### input

`````` [ 40585 ] is krishnamurthy number
[ 65 ] is not krishnamurthy number
[ 145 ] is krishnamurthy number``````
``````#  Ruby Program for
#  Krishnamurthy number
class FlaviusNumber
# Define the accessor and reader of class FlaviusNumber
attr_accessor :factorial
#  Auxiliary space to store factorial
def initialize()
self.factorial = Array.new(10) {0}
self.findFactorial()
end

def findFactorial()
i = 0
#  First factorial
self.factorial[0] = 1
#  Calculate the factorial of range (1..9)
i = 1
while (i <= 9)
self.factorial[i] = self.factorial[i - 1] * i
i += 1
end

end

#  Check whether given number is krishnamurthy number or not
def isKrishnamurthyNo(number)
if (number < 1)
return
end

#  Define some auxiliary variable
i = number
remainder = 0
sum = 0
#  Calculate digit factorial sum of given number
while (i != 0)
#  Get the last digit
remainder = i % 10
#  Factorial sum
sum += self.factorial[remainder]
#  Remove last digit
i = i / 10
end

if (sum == number)
#  When result is krishnamurthy number
print(" [", number ,"] is krishnamurthy number", "\n")
else
print(" [", number ,"] is not krishnamurthy number", "\n")
end

end

end

def main()
#  Test case
end

main()``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number
``````
``````/*
Scala Program for
Krishnamurthy number
*/
class FlaviusNumber(var factorial: Array[Int])
{
def this()
{
this(Array.fill[Int](10)(0));
this.findFactorial();
}
def findFactorial(): Unit = {
var i: Int = 0;
// First factorial
factorial(0) = 1;
// Calculate the factorial of range (1..9)
i = 1;
while (i <= 9)
{
factorial(i) = factorial(i - 1) * i;
i += 1;
}
}
// Check whether given number is krishnamurthy number or not
def isKrishnamurthyNo(number: Int): Unit = {
if (number < 1)
{
return;
}
// Define some auxiliary variable
var i: Int = number;
var remainder: Int = 0;
var sum: Int = 0;
// Calculate digit factorial sum of given number
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += factorial(remainder);
// Remove last digit
i = (i / 10).toInt;
}
if (sum == number)
{
// When result is krishnamurthy number
println(" [" + number + "] is krishnamurthy number");
}
else
{
println(" [" + number + "] is not krishnamurthy number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FlaviusNumber = new FlaviusNumber();
// Test case
}
}``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````
``````/*
Swift 4 Program for
Krishnamurthy number
*/
class FlaviusNumber
{
// Auxiliary space to store factorial
var factorial: [Int];
init()
{
self.factorial = Array(repeating: 0, count: 10);
self.findFactorial();
}
func findFactorial()
{
var i: Int = 0;
// First factorial
self.factorial[0] = 1;
// Calculate the factorial of range (1..9)
i = 1;
while (i <= 9)
{
self.factorial[i] = self.factorial[i - 1] * i;
i += 1;
}
}
// Check whether given number is krishnamurthy number or not
func isKrishnamurthyNo(_ number: Int)
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
var i: Int = number;
var remainder: Int = 0;
var sum: Int = 0;
// Calculate digit factorial sum of given number
while (i  != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += self.factorial[remainder];
// Remove last digit
i = i / 10;
}
if (sum == number)
{
// When result is krishnamurthy number
print(" [", number ,"] is krishnamurthy number");
}
else
{
print(" [", number ,"] is not krishnamurthy number");
}
}
}
func main()
{
// Test case
}
main();``````

#### input

`````` [ 40585 ] is krishnamurthy number
[ 65 ] is not krishnamurthy number
[ 145 ] is krishnamurthy number``````
``````/*
Kotlin Program for
Krishnamurthy number
*/
class FlaviusNumber
{
// Auxiliary space to store factorial
var factorial: Array < Int > ;

constructor()
{
this.factorial = Array(10)
{
0
};
this.findFactorial();
}
fun findFactorial(): Unit
{
var i: Int = 1;
// First factorial
this.factorial[0] = 1;
while (i <= 9)
{
this.factorial[i] = this.factorial[i - 1] * i;
i += 1;
}
}
// Check whether given number is krishnamurthy number or not
fun isKrishnamurthyNo(number: Int): Unit
{
if (number < 1)
{
return;
}
// Define some auxiliary variable
var i: Int = number;
var remainder: Int ;
var sum: Int = 0;
while (i != 0)
{
// Get the last digit
remainder = i % 10;
// Factorial sum
sum += this.factorial[remainder];
// Remove last digit
i = i / 10;
}
if (sum == number)
{
// When result is krishnamurthy number
println(" [" + number + "] is krishnamurthy number");
}
else
{
println(" [" + number + "] is not krishnamurthy number");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test case
}``````

#### input

`````` [40585] is krishnamurthy number
[65] is not krishnamurthy number
[145] is krishnamurthy number``````

## Resultant Output Explanation

The given code contains a `main` function that tests the `isKrishnamurthyNo` function with three test cases: 40585, 65, and 145.

1. For the input 40585, the function calculates the sum of the factorials of its digits as follows: 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585 Since the sum (40585) is equal to the original number (40585), it is a Krishnamurthy number. The output states that "40585 is a Krishnamurthy number."

2. For the input 65, the function calculates the sum of the factorials of its digits as follows: 6! + 5! = 720 + 120 = 840 Since the sum (840) is not equal to the original number (65), it is not a Krishnamurthy number. The output states that "65 is not a Krishnamurthy number."

3. For the input 145, the function calculates the sum of the factorials of its digits as follows: 1! + 4! + 5! = 1 + 24 + 120 = 145 Since the sum (145) is equal to the original number (145), it is a Krishnamurthy number. The output states that "145 is a Krishnamurthy number."

## Time Complexity of the Code

1. Calculating the factorials from 1 to 9 takes O(9) time, which simplifies to O(1) since it's a constant operation.

2. The while loop iterates through the digits of the input `number`, and the number of digits in an integer `n` is given by log10(n). Therefore, the loop runs O(log10(number)) times.

3. Within the loop, each iteration involves modulo, division, and array look-up operations, which all take constant time.

Thus, the overall time complexity of the code is O(log10(number)), where 'number' is the input integer. This complexity indicates that the program's performance is efficient, as it scales logarithmically with the size of the input.

## 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