Check Krishnamurthy Number in Java

In this post, we will write a Java program to check whether the given number is Krishnamurthy Number or not? We will also develop a Java program to display all the Krishnamurthy numbers which exist in the given range/interval.

What is a Krishnamurthy Number?

If the sum of the factorial of all digits of a number is equal to the original number then the number is called Krishnamurthy Number. For example- 145 and 40585 are Krishnamurthy numbers. 1 and 2 are also Krishnamurthy numbers because their factorials are equal to the same number.

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

The sum of the factorial of individual digits is the same as the original number 145. Hence, 145 is a Krishnamurthy number.

Similarly,
40585
=> 4! + 0! + 5! + 8! + 5!
=> 24 +1 + 120 + 40320 + 120
=> 40585

1! = 1
So, 1 is a Krishnamurthy number.

Similarly,
2! = 1*2 = 2
2 is also a Krishnamurthy number.

Hence the numbers 1, 2, 145 and 40585 are Krishnamurthy number.

Krishnamurthy Number

Note:- Sometimes the Krishnamurthy number is also called Strong number, Special number, and Peterson number.

Algorithm

  1. Using one temporary variable temp, store a copy of the original number.
  2. Initialize the variable sum with zero. It will store the sum of the factorial of each individual digits.
  3. Using loop until temp is not equal to zero,
    3.a) Get the last digit of variable temp, and store it in the variable currentDigit.
    3.b) Calculate the factorial of variable currentDigit.
    3.c) Add the factorial result into the variable sum.
    3.d) Remove the last digit of the number.
  4. Compare the original number with the value of the variable sum. If both are equal then the number is Krishnamurthy Number, else it is not.

Krishnamurthy Number program in Java

import java.util.Scanner;

public class KrishnamurthyNumber {

   // method to Check the Krishnamurthy number
   public static boolean isKrishnamurthy(int number){

      // declare variables
      int sum = 0, lastDigit = 0;
      int tempNum = number;

      // traverse through all digits of number
      while(tempNum != 0) {
          lastDigit = tempNum % 10;
          sum += factorial(lastDigit);
          tempNum /= 10;
      }

      // compare sum and number
      if(sum == number)
          return true; 
      return false; 
   }

   // method to calculate factorial of an integer
   public static long factorial(int n) {
      long fact = 1;
      for(int i=1; i<=n; i++) {
          fact *= i;
      }
      return fact;
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // check number is Krishnamurthy number
      result = isKrishnamurthy(number);
      if(result)
          System.out.println(number +
              " is a Krishnamurthy number.");
      else
          System.out.println(number +
              " is not a Krishnamurthy number.");

      // close Scanner class object
      scan.close();
   }
}

The output for the different test cases are:-

Enter an integer number:: 146
146 is not a Krishnamurthy number.

Enter an integer number:: 145
145 is a Krishnamurthy number.

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Optimization

We can analyze that the last digit will be always from 0 to 9, and every time we need to find the factorial from 0 to 9 only. So, it is a better idea to calculate the factorial value from 0 to 9 and store it in an array. For the large numbers, It will be an optimized solution for checking the number is a Krishnamurthy number or not. Conclusion:- before checking the number is a Krishnamurthy number or not, calculate factorial from 0 to 9 and store it in an array.

The static block executes before executing the main method so, the factorial values from 0 to 9 should be calculated and stored in the array fact[].

In the below Java program, we used this optimized solution to check the number is a Krishnamurthy number or not.

import java.util.Scanner;

public class KrishnamurthyNumber{

    // Array to store factorial value 
    // from 0 to 9
    static int fact[] = new int[10];

    // static block to calculate factorial
    static {
	    fact[0] = fact[1] = 1;
	    for(int i=2; i<fact.length; ++i)
            fact[i] = fact[i-1] * i;
    }

    // method to Check Krishnamurthy number
    public static boolean isKrishnamurthy(int number) {

        // declare variables
        int sum = 0, lastDigit = 0;
	int tempNum = number;

	// traverse through all digits of number
	while(tempNum != 0) {
	   lastDigit = tempNum % 10;
	   sum += fact[lastDigit];
	   tempNum /= 10;
        }

        // compare sum and number
        return (sum == number);
    }

    // main method
    public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);
      // take input from end-user
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check number is Krishnamurthy number?
      result = isKrishnamurthy(number);
      if(result)
          System.out.println(number +
                  " is a Krishnamurthy number.");
      else
          System.out.println(number +
                 " is not a Krishnamurthy number.");

      // close Scanner class object
      scan.close();
   }
}

Java program to find all the Krishnamurthy number in a Range

import java.util.Scanner;

public class KrishnamurthyNumberOptimized {

   // Array to store factorial value 
   // from 0 to 9
   static int fact[] = new int[10];

   // static block to calculate factorial
   static {
     fact[0] = fact[1] = 1;
     for(int i=2; i<fact.length; ++i)
     fact[i] = fact[i-1] * i;
   }

   // method to Check Krishnamurthy number
   public static boolean isKrishnamurthy(int number) {

     // declare variables
     int sum = 0, lastDigit = 0;
     int tempNum = number;

     // traverse through all digits of number
     while(tempNum != 0) {
       lastDigit = tempNum % 10;
       sum += fact[lastDigit];
       tempNum /= 10;
     }

     // compare sum and number
     return (sum == number);
  }

  // main method
  public static void main(String[] args) {

     // declare variables
     int minRange = 0, maxRange = 0;

     //create Scanner class object to take input
     Scanner scan = new Scanner(System.in);
     System.out.print("Enter the min value of range:");
     minRange = scan.nextInt();
     System.out.print("Enter the max value of range:");
     maxRange = scan.nextInt();

     // loop
     System.out.println("The Krishnamurthy number from "+ 
             minRange + " to "+ maxRange+" are: ");

     for(int i=minRange; i<=maxRange; i++) {
         // check number
         if(isKrishnamurthy(i))
             System.out.print(i +" ");
     }

     // close Scanner class object
     scan.close();
  }
}

Output:-

Enter the min value of range:1
Enter the max value of range:1000000
The Krishnamurthy number from 1 to 1000000 are:
1 2 145 40585

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *