Peterson Number in Java: Understanding the Concept and Its Applications

Peterson number is a special kind of number in which the sum of factorials of its digits is equal to the number itself. These numbers are named after the American mathematician James Peterson. In this blog, we will discuss what Peterson numbers are and how to find them in Java.

What are Peterson Numbers?

A Peterson number is a number that satisfies the following condition: The sum of factorials of each digit of the number is equal to the number itself. For example, let’s take the number 145:

1! + 4! + 5! = 1 + 24 + 120 = 145

As you can see, the sum of factorials of each digit of the number (1, 4, and 5) is equal to the number itself (145). Hence, 145 is a Peterson number.

Method to find Peterson Number in Java

There is one method to find Peterson numbers in Java. Let’s discuss two of them.

Using a User-Defined Function

We can write a user-defined function that takes a number as an argument and returns true if the number is a Peterson number, else false. Here’s the Java code for the same:

// Peterson Number in java
import java.util.*;
class Main
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
	//taking input from the user
	System.out.println("Enter the number");
		int n=sc.nextInt();
		int temp=n;//storing the number in a temporary variable
		int o=1,sum=0;
		while(n!=0)//running while loop until number becomes zero
		{
			o=1;
		//extracting last digit of the number
		//and storing in r
			int r=n%10;
		//for loop to find the factorial of a digit
			for(int i=1;i<=r;i++)
			{
				o=o*i;
			}
			sum=sum+o;//adding the factotial of the digits
			n=n/10;
		}
	//checking if the sum of the factorial of digits
	//is equal to the number or not
		if(sum==temp)
		System.out.println("PETERSON NUMBER");
		else
		System.out.println("NOT PETERSON NUMBER");
	}
	}

Output:

Enter the number
50
NOT PETERSON NUMBER
Enter the number
145
PETERSON NUMBER

How to Check for Peterson Number

A Peterson number is a number that is equal to the sum of factorials of each of its digits in the given base. In this blog post, we will discuss two methods to check for Peterson Number in Java.

A. Using the Formula

  • Description of Formula

The formula to check for Peterson number is as follows:

N = abcd….(n digits)

If N is a Peterson number, then N = a! + b! + c! + d! + ….

Where a, b, c, d, … are the digits of N.

  • Step-by-step guide to checking for Peterson Number using the formula

i. Extract the digits of the given number.

ii. Compute the factorial of each digit using a factorial function.

iii. Compute the sum of the factorials of each digit.

iv. Check if the sum of the factorials is equal to the given number.

v. If the sum of the factorials is equal to the given number, then the number is a Peterson number.

B. Using Loops

  • Explanation of Loops

In this method, we use a loop to extract each digit of the given number, compute its factorial and add it to a sum variable. We repeat this process until we have extracted all the digits of the number.

  • Step-by-step guide to checking for Peterson Number using loops

i. Extract the digits of the given number using the modulo operator.

ii. Compute the factorial of each digit using a factorial function.

iii. Add the factorial of each digit to a sum variable.

iv. Repeat steps i-iii until we have extracted all the digits of the number.

v. Check if the sum of the factorials is equal to the given number.

vi. If the sum of the factorials is equal to the given number, then the number is a Peterson number.

By using either of these methods, we can easily check whether a given number is a Peterson number or not.

Implementation of Peterson Number in Java

Java is an object-oriented, high-level programming language that is widely used for developing desktop, web, and mobile applications. Java is platform-independent, which means that the code written in Java can run on any platform without the need for recompilation.

The Peterson number program in Java takes an input number from the user and checks whether it is a Peterson number or not using the formula or loops. If the input number is a Peterson number, the program outputs a message indicating that the number is a Peterson number; otherwise, it outputs a message indicating that the number is not a Peterson number.

Step-by-step guide to implementing Peterson Number in Java

1.Define the class and main method:

public class PetersonNumber {
    public static void main(String[] args) {
        // code to check for Peterson number
    }
}

2.Declare and initialize the input number variable:

int num = 150;

3.Using Formula method:

i. Declare and initialize variables to store the sum of the factorial of each digit and the input number itself:

int sum = 0;
int temp = num;

ii. Use a loop to calculate the sum of the factorial of each digit:

while (temp > 0) {
    int digit = temp % 10;
    int fact = 1;
    for (int i = digit; i >= 1; i--) {
        fact *= i;
    }
    sum += fact;
    temp /= 10;
}

iii. Compare the sum with the input number to check if it is a Peterson number:

if (sum == num) {
    System.out.println(num + " is a Peterson number");
} else {
    System.out.println(num + " is not a Peterson number");
}

4.Using Loops method:

i. Declare and initialize variables to store the sum of the cubes of each digit and the input number itself:

int sum = 0;
int temp = num;

ii. Use a loop to calculate the sum of the cubes of each digit:

while (temp > 0) {
    int digit = temp % 10;
    sum += (digit * digit * digit);
    temp /= 10;
}

iii. Compare the sum with the input number to check if it is a Peterson number:

if (sum == num) {
    System.out.println(num + " is a Peterson number");
} else {
    System.out.println(num + " is not a Peterson number");
}

Sample Code for Peterson Number in Java

import java.util.Scanner;

public class PetersonNumber {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a number to check if it's a Peterson number: ");
        int num = in.nextInt();

        if (isPeterson(num))
            System.out.println(num + " is a Peterson number.");
        else
            System.out.println(num + " is not a Peterson number.");
    }

    private static boolean isPeterson(int num) {
        int sum = 0;
        int temp = num;

        while (temp > 0) {
            int digit = temp % 10;
            sum += factorial(digit);
            temp /= 10;
        }

        return sum == num;
    }

    private static int factorial(int num) {
        if (num == 0 || num == 1)
            return 1;

        int fact = 1;

        for (int i = 2; i <= num; i++) {
            fact *= i;
        }

        return fact;
    }
}

This code first takes input from the user to check if a number is a Peterson number or not. It then calls the isPeterson method to check if the number is a Peterson number or not. The isPeterson method uses a while loop to calculate the sum of the factorials of each digit in the given number. The factorial method is a separate method that calculates the factorial of a given number. Finally, the isPeterson method returns true if the sum of the factorials is equal to the given number, indicating that it is a Peterson number.

Applications of Peterson Number in Java

Peterson number is a special type of number that has unique properties. Although Peterson numbers may not have many direct applications, they are important for mathematical research and help in building more complex algorithms.

A.Use Cases of Peterson Number

Peterson numbers are used in various fields like computer science, data science, and cryptography. In computer science, Peterson numbers are used for designing algorithms for sorting and searching. In data science, Peterson numbers can be used for data analysis and pattern recognition.

B. Examples of Peterson Number in Real Life

Peterson numbers can be found in various natural phenomena, such as patterns in flowers and leaves. For example, the arrangement of petals in certain flowers, such as lilies and irises, follows a pattern of five. This pattern is related to the Fibonacci sequence and is also a Peterson number.

In cryptography, Peterson numbers can be used to generate random numbers for encryption and decryption. The randomness of the Peterson numbers makes it difficult for attackers to predict the key used for encryption.

In conclusion, Peterson Number is a fascinating concept in Mathematics that has several applications in various fields. It is interesting to note that even though Peterson Number was discovered relatively recently, it has already found several important applications. Therefore, understanding the properties and applications of Peterson Number is essential for anyone interested in Mathematics or related fields.

We hope you have gained more knowledge about “Peterson Number in Java” and other important information with the help of this article. Stay connected for more updates from Java Programming Blogs. You can also website our Newtum for more information about various programs such Core Python, DJANGO, HTML and more. 

About The Author

Leave a Reply