Python Program to Generate Strong Numbers in an Interval

In the previous article, we have discussed Python Program to Select a Random Element from a Tuple
Strong number:

A Strong number is a special number in which the total of all digit factorials equals the number itself.

Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125

To determine whether a given number is strong or not. We take each digit from the supplied number and calculate its factorial, we will do this for each digit of the number.

We do the sum of factorials once we have the factorial of all digits. If the total equals the supplied number, the given number is strong; otherwise, it is not.

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

Examples:

Example 1:

Input:

Given lower limit range  = 1
Given upper limit  range= 200

Output:

The Strong Numbers in a given range 1 and 200 are :
1 2 145

Example 2:

Input:

Given lower limit range = 100
Given upper limit range= 60000

Output:

The Strong Numbers in a given range 100 and 60000 are :
145 40585

Program to Generate Strong Numbers in an Interval

Below are the ways to generate Strong Numbers in a given interval.

Method #1: Using While loop and factorial() function (Static input)

Approach:

  1. Import math function using import keyword.
  2. Give the lower limit range as static input and store it in a variable.
  3. Give the upper limit range as static input and store it in another variable.
  4. Loop from lower limit range to upper limit range using For loop.
  5. Put the iterator value in a temporary variable called tempNum.
  6. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  7. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  8. Calculate the factorial of the last_Digit using math.factorial() function
  9. When the factorial of the last digit is found, it should be added to the totalSum = totalSumfactNum
  10. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  Itr = Itr /10
  11. Steps 5–8 should be repeated until N > 0.
  12. Check if the totalSum is equal to tempNum using the if conditional statement.
  13. If it is true then print the tempNum(which is the iterator value).
  14. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
# Give the lower limit range as static input and store it in a variable.
gvn_lower_lmt = 1
# Give the upper limit range as static input and store it in another variable.
gvn_upper_lmt = 200
# Loop from lower limit range to upper limit range using For loop.
print("The Strong Numbers in a given range",
      gvn_lower_lmt, "and", gvn_upper_lmt, "are :")
for itr in range(gvn_lower_lmt, gvn_upper_lmt+1):
   # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Put the iterator value in a temporary variable called tempNum.
    tempNum = itr
    # using while to extract digit by digit of the given iterator value
    while(itr):
        s = 1
        factNum = 1
        # Getting the last digit of the iterator value
        remainder = itr % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given itertor value by 10
        itr = itr//10
    # checking if the totalSum is equal to the iterator value
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        print(tempNum, end=' ')

Output:

The Strong Numbers in a given range 1 and 200 are :
1 2 145

Method #2: Using While loop and factorial() function (User input)

Approach:

  1. Import math function using the import keyword.
  2. Give the lower limit range as user input using int(input()) and store it in a variable.
  3. Give the upper limit range as user input using int(input()) and store it in another variable.
  4. Loop from lower limit range to upper limit range using For loop.
  5. Put the iterator value in a temporary variable called tempNum.
  6. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  7. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  8. Calculate the factorial of the last_Digit using math.factorial() function
  9. When the factorial of the last digit is found, it should be added to the totalSum = totalSumfactNum
  10. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  Itr = Itr /10
  11. Steps 5–8 should be repeated until N > 0.
  12. Check if the totalSum is equal to tempNum using the if conditional statement.
  13. If it is true then print the tempNum(which is the iterator value).
  14. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
#Give the lower limit range as user input using int(input()) and
#store it in a variable.
gvn_lower_lmt = int(input("Enter some random number = "))
#Give the upper limit range as user input using int(input()) and 
#store it in another variable.
gvn_upper_lmt = int(input("Enter some random number = "))
# Loop from lower limit range to upper limit range using For loop.
print("The Strong Numbers in a given range",
      gvn_lower_lmt, "and", gvn_upper_lmt, "are :")
for itr in range(gvn_lower_lmt, gvn_upper_lmt+1):
   # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Put the iterator value in a temporary variable called tempNum.
    tempNum = itr
    # using while to extract digit by digit of the given iterator value
    while(itr):
        s = 1
        factNum = 1
        # Getting the last digit of the iterator value
        remainder = itr % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given itertor value by 10
        itr = itr//10
    # checking if the totalSum is equal to the iterator value
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        print(tempNum, end=' ')

Output:

Enter some random number = 1
Enter some random number = 5000
The Strong Numbers in a given range 1 and 5000 are :
1 2 145

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.