Peterson Number in Java

In this post, Let us know about what is Peterson number and how can we check whether a given number is Peterson or not through a Java program.

What is Peterson Number?

A Peterson number is a unique type of number where the sum of the factorials of its digits is equal to the number itself. In other words, if we take each digit of the number, compute its factorial, and add them all together, the resulting sum will be equal to the original number.

Steps to Find Peterson Number

  • Read or input a number (n).
  • Extract the last digit (d) of the given number.
  • Calculate the factorial (fact) of the extracted digit.
  • Add the factorial (fact) to a variable that stores the sum.
  • Remove the last digit from the given number.
  • Repeat steps 2 to 5 until the given number becomes 0.
  • Once the loop is complete, compare the sum of factorials with the original number (n).
  • If the sum is equal to n, then the given number is a Peterson number. Otherwise, it is not.

Example of Peterson Number

Suppose, we have to check the number (n) 145 is Peterson or not.

Number = 145

145 = !1 + !4 + !5

=1+4*3*2*1+5*4*3*2*1

=1+24+120

145=145

In the given example, the number 145 is identified as a Peterson number. This classification is based on the observation that the number itself is equal to the sum of the factorials of its digits. In the case of 145, if we calculate the factorial of 1 (1!) and add it to the factorial of 4 (4!) and the factorial of 5 (5!), the resulting sum is 145, which matches the original number.

Peterson Number Java Program

PetersonNumberExample.java

import java.io.*;  
import java.util.*;  
public class PetersonNumberExample   
{  
//an array is defined for the quickly find the factorial  
static long[] factorial = new int[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};  
//driver code  
public static void main(String args[])  
{  
//constructor of the Scanner class  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number to check: ");  
//reading a number from the user  
int n=sc.nextInt();  
//calling the user-defined function to check Peterson number  
if (isPeterson(n))  
System.out.println("The given number is a Peterson number.");  
else  
System.out.println("The given number is not a Peterson number.");  
}  
//function to check the given number is Peterson or not  
static boolean isPeterson(int n)  
{  
int num = n;  
int sum = 0;  
//loop executes until the condition becomes false  
while (n > 0)   
{  
//determines the last digit of the given number      
int digit = n % 10;  
//determines the factorial of the digit and add it to the variable sum  
sum += factorial[digit];  
//removes the last digit of the given number  
n = n / 10;  
}  
//compares sum with num if they are equal returns the number itself  
return (sum == num);  
}  
}

Output 1:

Enter a number to check: 145
The given number is a Peterson number.

Output 2:

Enter a number to check: 773
The given number is not a Peterson number.

We hope that this article on the Peterson Number in Java has provided you with the necessary insights. For further similar content, please consider following tutorials.freshersnow.com.