 # Python Program to Find Strong Numbers in a List

In the previous article, we have discussed Python Program to Generate Strong Numbers in an Interval
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 list, and the task is to find all the Strong numbers in a given list.

Examples:

Example1:

Input:

Given List = [4, 1, 4, 145]

Output:

The Strong Numbers in a given List are :
1 145

Example2:

Input:

Given List =[5, 10, 650, 40585, 2, 145, 900]

Output:

The Strong Numbers in a given List are :
40585 2 145

## Program to Find Strong Numbers in a List

Below are the ways to find all the Strong numbers in a given list.

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

Approach:

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

Below is the implementation:

# Import the math module using import keyword.
import math
# Give the list as static input and store it in a variable.
list1 = [4, 1, 4, 145]
# Loop in the above given list using For Loop.
print("The Strong Numbers in a given List are :")
for i in list1:
# Taking a variable totalSum and initializing it with 0
totalSum = 0
# Put the iterator value in a temporary variable called tempNum.
tempNum = i
# using while to extract digit by digit of the given iterator value
while(i):
s = 1
factNum = 1
# Getting the last digit of the iterator value
remainder = i % 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
i = i//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 List are :
1 145

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

Approach:

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

Below is the implementation:

# Import the math module using import keyword.
import math
#Give the list as user input using list(),map(),input(),and split() functions and
#Store it in a variable
list1 = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Loop in the above given list using For Loop.
print("The Strong Numbers in a given List are :")
for i in list1:
# Taking a variable totalSum and initializing it with 0
totalSum = 0
# Put the iterator value in a temporary variable called tempNum.
tempNum = i
# using while to extract digit by digit of the given iterator value
while(i):
s = 1
factNum = 1
# Getting the last digit of the iterator value
remainder = i % 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
i = i//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 List Elements separated by spaces = 1 145 10 15 2
The Strong Numbers in a given List are :
1 145 2

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.