Program to check for Peterson number

A number is said to be a Peterson number if the sum of factorials of each digit of the number is equal to the number itself.

Example:

Input : n = 145
Output = Yes
Explanation:
 145 = 5! + 4! + 1!
     = 120 + 24 +1
     = 145

Input  : n = 55
Output : No
Explanation: 5! + 5!
            = 120 + 120
            = 240
Since 55 is not equal to 240
It is not a Peterson number.     

We will pick each digit (Starting from the last digit) of given number and find its factorial. And add all factorials. Finally we check if sum of factorials is equal to number or not.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to determine whether the number is
// Peterson number or not
#include <iostream>
using namespace std;
  
// To quickly find factorial of digits
int fact[10] = {1, 1, 2, 6, 24, 120, 720, 
                     5040, 40320, 362880};
  
// Function to check if a number is Peterson 
// or not
bool peterson(int n)
{
    int num = n, sum = 0;
  
    // stores the sum of factorials of 
    // each digit of the number.
    while (n > 0) {
        int digit = n % 10;
        sum += fact[digit];
        n = n / 10;        
    }
  
    // Condition check for a number to 
    // be a Peterson Number
    return (sum == n);
}
  
// Driver Program
int main()
{
    int n = 145;
    if (peterson(n))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to determine whether the
// number is Peterson number or not
import java.io.*;
  
public class GFG {
  
    // To quickly find factorial of digits
    static int []fact = new int[]{1, 1, 2,
                    6, 24, 120, 720, 5040,
                            40320, 362880};
      
    // Function to check if a number is
    // Peterson or not
    static boolean peterson(int n)
    {
        int num = n;
        int sum = 0;
      
        // stores the sum of factorials of 
        // each digit of the number.
        while (n > 0)
        {
            int digit = n % 10;
            sum += fact[digit];
            n = n / 10
        }
      
        // Condition check for a number to 
        // be a Peterson Number
        return (sum == num);
    }
      
    // Driver Program
    static public void main (String[] args)
    {
        int n = 145;
          
        if (peterson(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to determine whether the
# number is Peterson number or not
  
# To quickly find factorial of digits
fact = [1, 1, 2, 6, 24, 120, 720,
        5040, 40320, 362880]
  
# Function to check if a number
# is Peterson or not
def peterson (n):
    num = n
    sum = 0
      
    # stores the sum of factorials of 
    # each digit of the number.
    while n > 0:
        digit = int(n % 10)
        sum += fact[digit]
        n = int(n / 10)
      
    # Condition check for a number
    # to be a Peterson Number
    return (sum == num)
  
# Driver Code
n = 145
print("Yes" if peterson(n) else "No")
  
# This code is contributed by "Sharad_Bhardwaj"..

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to determine whether the
// number is Peterson number or not
using System;
  
public class GFG {
  
    // To quickly find factorial of digits
    static int []fact = new int[10]{1, 1, 2,
                      6, 24, 120, 720, 5040,
                             40320, 362880};
      
    // Function to check if a number is
    // Peterson or not
    static bool peterson(int n)
    {
        int num = n;
        int sum = 0;
      
        // stores the sum of factorials of 
        // each digit of the number.
        while (n > 0)
        {
            int digit = n % 10;
            sum += fact[digit];
            n = n / 10; 
        }
      
        // Condition check for a number to 
        // be a Peterson Number
        return (sum == num);
    }
      
    // Driver Program
    static public void Main ()
    {
        int n = 145;
          
        if (peterson(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to determine
// whether the number is
// Peterson number or not
  
// To quickly find 
// factorial of digits
$fact =array (1, 1, 2, 6, 24, 120, 720, 
                  5040, 40320, 362880);
  
// Function to check if 
// a number is Peterson 
// or not
function peterson($n)
{
    $num = $n; $sum = 0;
  
    // stores the sum of factorials of 
    // each digit of the number.
    while ($n > 0)
    {
        $digit = $n % 10;
        $n = $n / 10;     
    }
  
    // Condition check for
    // a number to be a
    // Peterson Number
    return ($sum == $n);
}
  
    // Driver Code
    $n = 145;
    if (peterson($n))
        echo "Yes";
    else
        echo"No";
      
// This code is contributed by ajit
?>

chevron_right



Output:

Yes


My Personal Notes arrow_drop_up

Lets get started

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, jit_t