Krishnamurthy Algorithm

The Krishnamurthy Algorithm, also known as the Krishnamurthy Number Algorithm, is a method used to identify a unique type of number known as a Krishnamurthy Number (or a Factorion). A Krishnamurthy Number is a natural number whose sum of the factorials of its individual digits is equal to the number itself. In other words, a number is considered a Krishnamurthy Number if the sum of the factorial of each digit within the number is equal to the original number. For example, the number 145 is a Krishnamurthy Number since 1! + 4! + 5! = 1 + 24 + 120 = 145. The Krishnamurthy Algorithm consists of a series of steps that, when followed, allow one to determine if a given number is a Krishnamurthy Number, or not. To begin, the algorithm decomposes the given number into its individual digits. Then, it calculates the factorial of each digit and adds these factorials together. Lastly, the algorithm compares the sum of the factorials to the original number. If the sum is equal to the original number, the number is a Krishnamurthy Number. If not, it is not a Krishnamurthy Number. This algorithm is particularly useful in number theory and recreational mathematics, as it provides a systematic approach to identifying and working with this unique class of numbers.
package Others;

import java.util.Scanner;

class Krishnamurthy {
    static int fact(int n) {
        int i, p = 1;
        for (i = n; i >= 1; i--)
            p = p * i;
        return p;
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int a, b, s = 0;
        System.out.print("Enter the number : ");
        a = sc.nextInt();
        int n = a;
        while (a > 0) {
            b = a % 10;
            s = s + fact(b);
            a = a / 10;
        }
        if (s == n)
            System.out.print(n + " is a krishnamurthy number");
        else
            System.out.print(n + " is not a krishnamurthy number");
        sc.close();
    }
}

LANGUAGE:

DARK MODE: