Python Program to Check if a Number is Peterson Number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Given a number, the task is to check whether the given number is a Peterson number or not.

Peterson Number:

The Peterson number is the number whose sum of factorials of each digit equals the number itself. Let me give you an example to help you understand:

Examples:

Example1:

Input:

Given number =1

Output:

The given number [ 1 ] is a Peterson number

Example2:

Input:

Given number =145

Output:

The given number [ 145 ] is a Peterson number

Program to Check if a Number is Peterson Number in Python

Below are the ways to check whether the given number is a Peterson number or not.

Method #1: Using For Loop (Static Input)

Approach:

  • Import the math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Take a variable psum and initialize its value to 0.
  • Convert the given number to a string using the str() function.
  • Convert the given number into a list of digits using list(),map(),int,split() functions.
  • Traverse in this list of digits using For loop.
  • Calculate the factorial of the list value using math.factorial() function.
  • Add this factorial value to psum.
  • Check if psum is equal to the given number using the If statement.
  • If it is true then it is a Peterson number.
  • Else it is not Peterson’s number.
  • The Exit of the Program.

Below is the implementation:

# Import the math module using the import keyword.
import math
# Give the number as static input and store it in a variable.4
gvnnumb = 145
# Take a variable psum and initialize its value to 0.
psum = 0
# Convert the given number to a string using the str() function.
strnumbe = str(gvnnumb)
# Convert the given number into a list of
# digits using list(),map(),int,split() functions.
lstdigts = list(map(int, strnumbe))
# Traverse in this list of digits using For loop.
for dgtnumbr in lstdigts:
    # Calculate the factorial of the list value using math.factorial() function.
    numbfact = math.factorial(dgtnumbr)
    # Add this factorial value to psum.
    psum = psum+numbfact
# Check if psum is equal to the given number using the If statement.
if(psum == gvnnumb):
        # If it is true then it is a Peterson number.
    print('The given number [', gvnnumb, '] is a Peterson number')
# Else it is not Peterson's number.
else:
    print('The given number [', gvnnumb, '] is not a Peterson number')

Output:

The given number [ 145 ] is a Peterson number

Method #2: Using For Loop (User Input)

Approach:

  • Import the math module using the import keyword.
  • Give the number as user input using int(input()) and store it in a variable.
  • Take a variable psum and initialize its value to 0.
  • Convert the given number to a string using the str() function.
  • Convert the given number into a list of digits using list(),map(),int,split() functions.
  • Traverse in this list of digits using For loop.
  • Calculate the factorial of the list value using math.factorial() function.
  • Add this factorial value to psum.
  • Check if psum is equal to the given number using the If statement.
  • If it is true then it is a Peterson number.
  • Else it is not Peterson’s number.
  • The Exit of the Program.

Below is the implementation:

# Import the math module using the import keyword.
import math
# Give the number as user input using int(input()) and store it in a variable.
gvnnumb = int(input(
    'Enter some random number to check whether the given number is a Peterson or not = '))
# Take a variable psum and initialize its value to 0.
psum = 0
# Convert the given number to a string using the str() function.
strnumbe = str(gvnnumb)
# Convert the given number into a list of
# digits using list(),map(),int,split() functions.
lstdigts = list(map(int, strnumbe))
# Traverse in this list of digits using For loop.
for dgtnumbr in lstdigts:
    # Calculate the factorial of the list value using math.factorial() function.
    numbfact = math.factorial(dgtnumbr)
    # Add this factorial value to psum.
    psum = psum+numbfact
# Check if psum is equal to the given number using the If statement.
if(psum == gvnnumb):
        # If it is true then it is a Peterson number.
    print('The given number [', gvnnumb, '] is a Peterson number')
# Else it is not Peterson's number.
else:
    print('The given number [', gvnnumb, '] is not a Peterson number')

Output:

Enter some random number to check whether the given number is a Peterson or not = 1
The given number [ 1 ] is a Peterson number

Related Programs: