Table of Contents
ToggleExploring Krishnamurthy Number in Different Programming Languages
Krishnamurthy numbers (strong numbers or factorial numbers) are fascinating mathematical numbers that have interesting properties.
We will first understand the concept of Krishnamurthy numbers.
Then we will see the interesting coding techniques to find them in different languages (like Java, C, C++, and Python). Let’s understand the logic behind the code in each programming languages.
What is Krishnamurthy’s Number?
A Krishnamurthy number is a +ve integer whose sum of the factorials of its digits is equal to the number itself. For instance, let’s consider the number
145: 1! + 4! + 5! = 1 + 24 + 120 = 145
Krishnamurthy Number in Java
Java is known in the world for the simplicity & readability of it’s programs. Here’s a program that checks whether a given number is a Krishnamurthy number in Java.
Step 1:- Define a function to calculate the factorial of a number.
Step 2: Implement a function to check if a number is a Krishnamurthy number.
Step 3: Test the implementation by calling the `isKrishnamurthy` function with different numbers.
This is a simple program that calculates Krishnamurthy Number in Java. It checks whether the sum of factorials of the digits is equal to that number or not. We can easily use this method on much bigger numbers.
Krishnamurthy Number in C
Here’s an example of a C program to determine if a number is a Krishnamurthy number:
To explain Krishnamurthy number in C code step by step, we can break down the process into smaller tasks:
- Include necessary libraries:
- Declare a function to calculate the factorial of a number:
- Declare the main function:
Step-by-step code:
- To use printf and scanf, we add `<stdio.h>`.
- We define `factorial` to compute the factorial of `n`. Recursion calculates factorial in this function.
- The `main` function declares `num`, `temp`, `digit`, and `sum`. `num` holds the user-entered positive integer. `temp` temporarily stores `num` to retrieve its digits. `digit` saves `temp`’s rightmost digit each iteration. Sum holds the digit factorials.
- We ask for a positive integer and ‘scanf’ it.
- To preserve input, we assign `num` to `temp`.
- We loop till ‘temp=0’. We save the rightmost digit of `temp` in `digit` each iteration using the modulus operator `%`. We add the factorial of `digit` to the `sum` using the `factorial` function. The rightmost digit is removed by dividing `temp` by 10.
- We compare `sum` to `num` after the loop. If equal, we report that `num` is a Krishnamurthy number. Otherwise, we report that `num` is not Krishnamurthy.
- Successful program execution returns ‘0’ from `main`.
The code will identify whether the number is Krishnamurthy.
Krishnamurthy Number in C++
Here’s an example of a program to check if a number is a Krishnamurthy number.
Let’s break down the code and explain each part in detail…
In this section, we include the necessary header file `iostream` for input/output operations and use the `std` namespace to avoid explicitly writing `std::` before standard library functions.
The ‘factorial’ function accepts an integer ‘num’ as an argument and returns that number’s factorial. The factorial is computed by recursion. The factorial is 1 if ‘num’ is either 0 or 1. If not, the function repeatedly multiplies itself by ‘num’ and calls itself with ‘num – 1’.
This function `isKrishnamurthy` checks if a given number `num` is a Krishnamurthy’s number. It takes the input number, `num`, and performs the following steps.
- It initializes `originalNum` with `num`’s original value for comparison.
- It initializes `sum` to 0 to compute the digit factorials.
- It enters a while loop until `num` is 0. Each time:
– The modulus operator `%` removes the rightmost digit of `num` and saves it in `digit`.
– It calculates the factorial of `digit` and adds it to `sum` using `factorial()`.
Divides `num` by 10 to eliminate the rightmost digit.
- After the `while` loop, it compares `originalNum` to `sum`. If they match, it returns “true,” indicating a Krishnamurthy’s number. False otherwise.
The program begins in main(). It executes:
- It stores user input in an integer variable `number`.
- `cout` displays “Enter a number:”.
- `cin` keeps user input in `number`.
- It calls `isKrishnamurthy()` with `number`.
- If `isKrishnamurthy()` returns `true`, `cout` prints the number.
- If `isKrishnamurthy()` returns `false`, `cout` reports that the number is not.
- It returns 0, signifying program completion.
To determine whether a user-entered number is a Krishnamurthy’s number, this code adds up its factorials.
Krishnamurthy Number in Python
Here’s an explanation of Krishnamurthy number in Python code step by step:
`factorial()` calculates the factorial of `num` in this code sample. Since the factorial of 0 and 1 equals 1, it yields 1. Otherwise, it recursively calls itself with `num – 1` and multiplies by `num` to compute the factorial.
`is_krishnamurthy()` tests whether `num` is a Krishnamurthy number. Function steps:
- It initializes `original_num` for subsequent comparison.
- It initializes `sum` to 0 to compute the digit factorials.
- It enters a while loop until `num` is 0. Each iteration: – It extracts the rightmost digit of `num` using the modulus operator `%` and puts it in `digit`.
– It calculates the factorial of `digit` and adds it to `sum` using `factorial()`.
Integer division `num //= 10` removes the rightmost digit.
- After the `while` loop, it compares `original_num` to `sum`. If they match, it returns “True,” indicating a Krishnamurthy number. False otherwise.
The `input()` function – enter a number, and `int()` converts it to an integer. The application then calls the is_krishnamurthy() method to see whether the number is Krishnamurthy.
If is_krishnamurthy() returns True, it prints that the number is a Krishnamurthy number. It prints “not a Krishnamurthy number” else.
This Python programme calculates the sum of factorials of a user-entered integer to determine whether it is a Krishnamurthy number.
Key Points
- Krishnamurthy Numbers: Sum of digit factorials equals the number.
- Languages Covered: Java, C, C++, Python.
- Language Breakdown: Code logic explained step by step.
- Key Points by Language: