克里希那穆尔数是一个数字,其阶乘的和等于该数字本身。例如145,每个数字的阶乘和:
1! + 4! + 5! = 1 + 24 + 120 = 145
例子:
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.
这个想法很简单,我们计算所有数字的阶乘和,然后将和与n进行比较。
C++
// C++ program to check if a number
// is a krishnamurthy number
#include
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.
PHP
Javascript
输出:
YES
有趣的是,我们确切地知道了四个Krishnamurthy数,即1、2、145和40585。