Check if a number is a Krishnamurthy Number or not
Last Updated :
07 Jan, 2024
A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.
For example, 145 is the sum of the factorial of each digit.
1! + 4! + 5! = 1 + 24 + 120 = 145
Examples:
Input : 145
Output : YES
Explanation: 1! + 4! + 5! =
1 + 24 + 120 = 145, which is equal to input,
hence YES.Input : 235
Output : NO
Explanation: 2! + 3! + 5! =
2 + 6 + 120 = 128, which is not equal to input,
hence NO.
The idea is simple, we compute the sum of factorials of all digits and then compare the sum with n.
C++
// C++ program to check if a number
// is a krishnamurthy number
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the factorial of any number
int factorial(int n)
{
int fact = 1;
while (n != 0) {
fact = fact * n;
n--;
}
return fact;
}
// function to Check if number is krishnamurthy
bool isKrishnamurthy(int n)
{
int sum = 0;
int temp = n;
while (temp != 0) {
// calculate factorial of last digit
// of temp and add it to sum
sum += factorial(temp % 10);
// replace value of temp by temp/10
temp = temp / 10;
}
// Check if number is krishnamurthy
return (sum == n);
}
// Driver code
int main()
{
int n = 145;
if (isKrishnamurthy(n))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java program to check if a number
// is a krishnamurthy number.
import java.util.*;
import java.io.*;
class Krishnamurthy {
// function to calculate the factorial
// of any number
static int factorial(int n)
{
int fact = 1;
while (n != 0) {
fact = fact * n;
n--;
}
return fact;
}
// function to Check if number is krishnamurthy
static boolean isKrishnamurthy(int n)
{
int sum = 0;
int temp = n;
while (temp != 0) {
// calculate factorial of last digit
// of temp and add it to sum
sum += factorial(temp % 10);
// replace value of temp by temp/10
temp = temp / 10;
}
// Check if number is krishnamurthy
return (sum == n);
}
// Driver code
public static void main(String[] args)
{
int n = 145;
if (isKrishnamurthy(n))
System.out.println("YES");
else
System.out.println("NO");
}
}
Python3
# Python program to check if a number
# is a krishnamurthy number
# function to calculate the factorial
# of any number
def factorial(n) :
fact = 1
while (n != 0) :
fact = fact * n
n = n - 1
return fact
# function to Check if number is
# krishnamurthy/special
def isKrishnamurthy(n) :
sum = 0
temp = n
while (temp != 0) :
# calculate factorial of last digit
# of temp and add it to sum
rem = temp%10
sum = sum + factorial(rem)
# replace value of temp by temp / 10
temp = temp // 10
# Check if number is krishnamurthy
return (sum == n)
# Driver code
n = 145
if (isKrishnamurthy(n)) :
print("YES")
else :
print("NO")
# This code is contributed by Prashant Aggarwal
C#
// C# program to check if a number
// is a krishnamurthy number.
using System;
class GFG {
// function to calculate the
// factorial of any number
static int factorial(int n)
{
int fact = 1;
while (n != 0) {
fact = fact * n;
n--;
}
return fact;
}
// function to Check if number is
// krishnamurthy
static bool isKrishnamurthy(int n)
{
int sum = 0;
int temp = n;
while (temp != 0) {
// calculate factorial of
// last digit of temp and
// add it to sum
sum += factorial(temp % 10);
// replace value of temp
// by temp/10
temp = temp / 10;
}
// Check if number is
// krishnamurthy
return (sum == n);
}
// Driver code
public static void Main()
{
int n = 145;
if (isKrishnamurthy(n))
Console.Write("YES");
else
Console.Write("NO");
}
}
// This code is contributed by nitin mittal.
JavaScript
<script>
// Javascript program to check if a number
// is a krishnamurthy number
// Function to find Factorial
// of any number
function factorial(n)
{
let fact = 1;
while (n != 0)
{
fact = fact * n;
n--;
}
return fact;
}
// Function to check if number
// is krishnamurthy
function isKrishnamurthyNumber(n)
{
let sum = 0;
// Storing the value in
// other variable
let temp = n;
while (temp != 0)
{
// Calculate factorial of last digit
// of temp and add it to sum
sum = sum + factorial(temp % 10);
// Integer Division
// replace value of temp by temp/10
temp = parseInt(temp / 10);
}
// Check if number is krishnamurthy
return sum == n;
}
// Driver code
let n = 145;
if (isKrishnamurthyNumber(n))
document.write("YES");
else
document.write("NO");
// This code is contributed by _saurabh_jaiswal
</script>
PHP
<?php
// PHP program to check if a number
// is a krishnamurthy number
// Function to find Factorial
// of any number
function factorial($n)
{
$fact = 1;
while($n != 0)
{
$fact = $fact * $n;
$n--;
}
return $fact;
}
// function to Check if number
// is krishnamurthy
function isKrishnamurthyNumber($n)
{
$sum = 0;
// Storing the value in
// other variable
$temp = $n;
while($temp != 0)
{
// calculate factorial of last digit
// of temp and add it to sum
$sum = $sum + factorial($temp % 10);
// Integer Division
// replace value of temp by temp/10
$temp = intdiv($temp, 10);
}
// Check if number is krishnamurthy
return $sum == $n;
}
// Driver code
$n = 145;
if (isKrishnamurthyNumber($n))
echo "YES";
else
echo "NO";
// This code is contributed by akash7981
?>
Time Complexity: O(n log10n) where n is a given number
Auxiliary Space: O(1)
Interestingly, there are exactly four Krishnamurthy numbers i.e. 1, 2, 145, and 40585 known to us.
Approach 2: Precomputing factorials and checking each digit of the number against the precomputed factorials.
- The declaration int factorial[10]; creates an array factorial of 10 integers to store the precomputed factorials.
- The precomputeFactorials() function calculates and stores the factorials of numbers 0 to 9 in the factorial array. It uses a for loop to iterate through each number and calculates its factorial by multiplying it with the factorial of the previous number.
- The isKrishnamurthy(int n) function takes an integer n as input and checks if it is a Krishnamurthy number or not. It first declares a variable sum to store the sum of factorials of digits in n and a variable temp to store a copy of n.
- It then enters a while loop that continues until temp becomes zero. In each iteration of the loop, it calculates the rightmost digit of temp using the modulo operator (temp % 10) and adds the factorial of that digit to sum. It then updates the value of temp by removing the rightmost digit using integer division (temp /= 10).
- After the while loop completes, the function returns true if sum is equal to n, indicating that n is a Krishnamurthy number, or false otherwise.
- In the main() function, we call precomputeFactorials() to precompute the factorials of numbers 0 to 9 and store them in the factorial array.
- We then set n to 145, which is a Krishnamurthy number, and call isKrishnamurthy(n) to check if n is a Krishnamurthy number or not.
- Finally, we use cout to print "YES" if isKrishnamurthy(n) returns true, indicating that n is a Krishnamurthy number, or "NO" otherwise. We also use endl to insert a newline character after the output.
C++
#include <iostream>
using namespace std;
int factorial[10];
void precomputeFactorials() {
factorial[0] = 1;
for (int i = 1; i < 10; i++) {
factorial[i] = i * factorial[i-1];
}
}
bool isKrishnamurthy(int n) {
int sum = 0;
int temp = n;
while (temp > 0) {
int digit = temp % 10;
sum += factorial[digit];
temp /= 10;
}
return (sum == n);
}
int main() {
precomputeFactorials();
int n = 145;
if (isKrishnamurthy(n)) {
cout <<"YES" << endl;
} else {
cout <<"NO" << endl;
}
return 0;
}
Java
// This is the Java code which checks if a given
// number is a Krishnamurthy number or not.
//A Krishnamurthy number is a number whose sum of factorials
// of its digits is equal to the number itself
import java.util.Arrays;
public class KrishnamurthyNumber {
// Declare an array to store factorials of digits 0-9
static int[] factorial = new int[10];
// Function to precompute the factorials and store them
// in the array
public static void precomputeFactorials()
{
factorial[0] = 1;
for (int i = 1; i < 10; i++) {
factorial[i] = i * factorial[i - 1];
}
}
// Function to check if a given number is a
// Krishnamurthy number or not
public static boolean isKrishnamurthy(int n)
{
int sum = 0;
int temp = n;
// Compute the sum of factorials of digits of the
// number
while (temp > 0) {
int digit = temp % 10;
sum += factorial[digit];
temp /= 10;
}
// Check if the sum of factorials is equal to the
// input number
return (sum == n);
}
public static void main(String[] args)
{
// Compute the factorials using
// precomputeFactorials() function
precomputeFactorials();
int n = 145;
// Check if 145 is a Krishnamurthy number using
// isKrishnamurthy() function
if (isKrishnamurthy(n)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
Python3
# Precompute factorials of digits 0-9
factorial = [1] * 10
def precompute_factorials():
for i in range(1, 10):
factorial[i] = i * factorial[i-1]
precompute_factorials()
# Check if a number is a Krishnamurthy number
def is_krishnamurthy(n):
# Compute sum of factorials of digits
sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += factorial[digit]
temp //= 10
# Check if sum equals the original number
return (sum == n)
# Test if a given number is a Krishnamurthy number
n = 145
if is_krishnamurthy(n):
print("YES")
else:
print("NO")
C#
using System;
namespace KrishnamurthyNumber
{
class Program
{
static int[] factorial = new int[10];
static void precomputeFactorials()
{
factorial[0] = 1;
for (int i = 1; i < 10; i++)
{
factorial[i] = i * factorial[i - 1];
}
}
static bool isKrishnamurthy(int n)
{
int sum = 0;
int temp = n;
while (temp > 0)
{
int digit = temp % 10;
sum += factorial[digit];
temp /= 10;
}
return (sum == n);
}
static void Main(string[] args)
{
precomputeFactorials();
int n = 145;
if (isKrishnamurthy(n))
{
Console.WriteLine("YES");
}
else
{
Console.WriteLine("NO");
}
}
}
}
JavaScript
let factorial = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
function precomputeFactorials() {
for (let i = 1; i < 10; i++) {
factorial[i] = i * factorial[i-1];
}
}
function isKrishnamurthy(n) {
let sum = 0;
let temp = n;
while (temp > 0) {
let digit = temp % 10;
sum += factorial[digit];
temp = Math.floor(temp / 10);
}
return (sum === n);
}
precomputeFactorials();
let n = 145;
if (isKrishnamurthy(n)) {
console.log("YES");
} else {
console.log("NO");
}
Time Complexity: O(logN)
Auxiliary Space: O(1)
Similar Reads
Factorial of a Number Given the number n (n >=0), find its factorial. Factorial of n is defined as 1 x 2 x ... x n. For n = 0, factorial is 1. We are going to discuss iterative and recursive programs in this post.Examples:Input: n = 5Output: 120Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120Input: n = 4Output: 24Explanation
7 min read
Legendre's formula - Largest power of a prime p in n! Given an integer n and a prime number p, the task is to find the largest x such that px (p raised to power x) divides n!.Examples: Input: n = 7, p = 3Output: x = 2Explanation: 32 divides 7! and 2 is the largest such power of 3.Input: n = 10, p = 3Output: x = 4Explanation: 34 divides 10! and 4 is the
6 min read
Count trailing zeroes in factorial of a number Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5Output: 1 Explanation: Factorial of 5 is 120 which has one trailing 0.Input: n = 20Output: 4Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.Input: n = 100Output: 2
8 min read
Find the Factorial of a large number Factorial of a non-negative integer, is the multiplication of all integers smaller than or equal to n. Factorial of a numberExamples: Input: 100Output: 933262154439441526816992388562667004- 907159682643816214685929638952175999- 932299156089414639761565182862536979- 2082722375825118521091686400000000
15+ min read
Primorial of a number Given a number n, the task is to calculate its primorial. Primorial (denoted as Pn#) is a product of first n prime numbers. Primorial of a number is similar to the factorial of a number. In primorial, not all the natural numbers get multiplied only prime numbers are multiplied to calculate the primo
10 min read
Find maximum power of a number that divides a factorial Given two numbers, fact and n, find the largest power of n that divides fact! (Factorial of fact). Examples:Â Input : fact = 5, n = 2 Output : 3 Explanation: Value of 5! is 120. The largest power of 2 that divides 120 is 8 (or 23 Input : fact = 146, n = 15 Output : 35 The idea is based on Legendreâs
12 min read
Largest power of k in n! (factorial) where k may not be prime Given two positive integers k and n, where k > 1, find the largest power of k that divides n! (n factorial).Examples: Input: n = 7, k = 2Output: 4Explanation: 7! = 5040, and 24 = 16 is the highest power of 2 that divides 5040.Input: n = 10, k = 9Output: 2Explanation: 10! = 3628800, and 9² = 81 is
15+ min read
Check if a number is a Krishnamurthy Number or not A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.For example, 145 is the sum of the factorial of each digit.1! + 4! + 5! = 1 + 24 + 120 = 145 Examples: Input : 145Output : YESExplanation: 1! + 4! + 5! = 1 + 24 + 120 = 145, which is equal to input,
10 min read
Last non-zero digit of a factorial Given a number n, find the last non-zero digit in n!.Examples: Input : n = 5 Output : 2 5! = 5 * 4 * 3 * 2 * 1 = 120 Last non-zero digit in 120 is 2. Input : n = 33 Output : 8 Recommended PracticeLast non-zero digit in factorialTry It! A Simple Solution is to first find n!, then find the last non-ze
14 min read
Count digits in a factorial using Logarithm Given an integer N, find the number of digits that appear in its factorial, where factorial is defined as, factorial(n) = 1*2*3*4........*n and factorial(0) = 1Examples : Input: 5Output: 3Explanation: 5! = 120, i.e., 3 digitsInput: 10Output: 7Explanation: 10! = 3628800, i.e., 7 digits Naive approach
8 min read