Krishnamurthy Number: Mathematical Equation

Algorithms

Krishnamurthy Number

A Krishnamurthy number, also known as a strong number or a digit factorial, is a number that is equal to the sum of the factorials of its digits. More formally, a number n is a Krishnamurthy number if the sum of the factorials of its digits is equal to n.

let's consider the number 145:

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

Note: Not all numbers are Krishnamurthy numbers. It's an interesting property that some numbers possess, and they are relatively rare.

Mathematical Equation:

Let's break down the mathematical equation that defines a Krishnamurthy number. Given a number n with digits d1,d2,…,dk, the sum of the factorials of its digits is calculated as follows:

d1! + d2! + … + dk!

Here, di represents the individual digits of the number. The factorial of a digit di is the product of all positive integers less than or equal to di. For example, 4! is equal to 4×3×2×1=24.

If the sum of the factorials of the digits is equal to the original number n, then n is considered a Krishnamurthy number. Mathematically, it can be expressed as:

d1! + d2! + … + dk! = n

For instance, in the example of the number 145:

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

Since the sum of the factorials of its digits (1, 4, and 5) is equal to the number itself (145), 145 is a Krishnamurthy number. This property is an interesting mathematical concept that leads to a set of specific numbers with this factorial sum property.

Few examples of Krishnamurthy numbers:

1. 1:
1! = 1

2. 2:
2! = 2 × 1 = 2

3. 145:
1! + 4! + 5! = 1 + 24 + 120 = 145

4. 40585:
4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585

These numbers exhibit the property that the sum of the factorials of their digits is equal to the number itself, making them Krishnamurthy numbers.

Programming Structure:

Here's a simple C program to check whether a given number is a Krishnamurthy number: The program structure includes user input, function definitions for factorial calculation and Krishnamurthy number checking, and the main function for executing the overall logic.

c Copy Code
#include <stdio.h>

// Function to calculate the factorial of a number
int factorial(int num) {
    if (num == 0 || num == 1) {
        return 1;
    } else {
        return num * factorial(num - 1);
    }
}

// Function to check if a number is a Krishnamurthy number
int is_krishnamurthy(int number) {
    int original_number = number;
    int digit_sum = 0;

    while (number > 0) {
        int digit = number % 10;
        digit_sum += factorial(digit);
        number /= 10;
    }

    return digit_sum == original_number;
}

int main() {
    // Input from the user
    int user_input;
    printf("Enter a number: ");
    scanf("%d", &user_input);

    // Check if the input number is a Krishnamurthy number
    if (is_krishnamurthy(user_input)) {
        printf("%d is a Krishnamurthy number.\n", user_input);
    } else {
        printf("%d is not a Krishnamurthy number.\n", user_input);
    }

    return 0;
}
Output:
Enter a number: 145
145 is a Krishnamurthy number.

Let's break down the provided C program:

1. Factorial Function:

int factorial(int num) {
    if (num == 0 || num == 1) {
        return 1;
    } else {
        return num * factorial(num - 1);
    }
}                                

This function calculates the factorial of a given number using recursion. It returns 1 for input 0 or 1, and for other values, it multiplies the number with the factorial of '(number - 1)'.

2. is_krishnamurthy Function:

int is_krishnamurthy(int number) {
    int original_number = number;
    int digit_sum = 0;

    while (number > 0) {
        int digit = number % 10;
        digit_sum += factorial(digit);
        number /= 10;
    }

    return digit_sum == original_number;
}

This function checks whether a given number is a Krishnamurthy number. It iterates through each digit of the number, calculates the factorial of the digit, and adds it to 'digit_sum'. The function returns true if 'digit_sum' is equal to the original number, indicating that it's a Krishnamurthy number.

3. Main Function:

int main() {
    int user_input;
    printf("Enter a number: ");
    scanf("%d", &user_input);

    if (is_krishnamurthy(user_input)) {
        printf("%d is a Krishnamurthy number.\n", user_input);
    } else {
        printf("%d is not a Krishnamurthy number.\n", user_input);
    }

    return 0;
}

This is the main function. It takes user input for a number, calls the 'is_krishnamurthy' function to check if it's a Krishnamurthy number, and prints the result accordingly.

In conclusion, Krishnamurthy numbers, also known as strong numbers or digit factorials, possess a fascinating mathematical property. These numbers are defined by the sum of the factorials of their individual digits being equal to the number itself. The exploration of Krishnamurthy numbers provides insights into both mathematical patterns and programming logic.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.