Comprobar si un número es un número de Krishnamurthy o no

Un número de Krishnamurthy es un número cuya suma del factorial de dígitos es igual al número mismo. Por ejemplo 145, suma de factorial de cada dígito: 
1! + 4! + 5! = 1 + 24 + 120 = 145

Ejemplos: 

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.

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
?>

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>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *