Print all Strong Numbers in Given List - Python
Last Updated :
08 Feb, 2025
The task of printing all Strong numbers from a given list in Python involves iterating through the list and checking each number based on its digit factorial sum. A Strong number is a number whose sum of the factorials of its digits equals the number itself. For example, given a list a = [145, 375, 100, 2, 10, 40585, 0], the goal is to identify and print numbers like 145 and 40585, where 1! + 4! + 5! = 145 and 4! + 0! + 5! + 8! + 5! = 40585.
Using precomputed factorials
In this method, we store the factorials of digits (0-9) in advance using a dictionary. This allows us to quickly check if a number is strong without recalculating factorials, making it the fastest approach.
Python
from math import factorial
# Precompute factorials for digits 0-9
fact = {i: factorial(i) for i in range(10)}
a = [145, 375, 100, 2, 10, 40585, 0]
res = [num for num in a if sum(fact[int(digit)] for digit in str(num)) == num]
print(res)
Explanation:
- {i: factorial(i) for i in range(10)} iterates over digits 0-9, calculates their factorial using factorial(i) and stores them as key-value pairs. List comprehension filters Strong numbers by iterating over each number’s digits, fetching their precomputed factorials, summing them and checking if the sum equals the original number if true, it’s included in res.
Using math.factorial()
This method uses Python’s built-in math.factorial() compute factorials directly. It simplifies the code but recalculates factorials every time, making it slightly slower than precomputed factorials.
Python
from math import factorial
a = [145, 375, 100, 2, 10, 40585, 0]
res = [num for num in a if sum(factorial(int(digit)) for digit in str(num)) == num]
print(res)
Explanation: List comprehension iterates through each number, extracts its digits, computes their factorials using math.factorial(int(digit)), sums them and includes the number in res if the sum matches the original number.
Using iterative factorial calculation
Here, we define a function to calculate factorials using a loop. This method avoids using external libraries but is slower because it recalculates factorials repeatedly.
Python
def factorial(n):
fact = 1
for i in range(2, n + 1):
fact *= i
return fact
def is_strong(n):
return sum(factorial(int(digit)) for digit in str(n)) == n
a = [145, 375, 100, 2, 10, 40585, 0]
res = [num for num in a if is_strong(num)]
print(res)
Explanation: factorial(n) function calculates a number’s factorial iteratively, while is_strong(n) checks if a number is Strong by summing the factorials of its digits. The list comprehension filters Strong numbers from a using is_strong(num), storing the results in res .
Using nested for loop
This method manually computes factorials using a nested loop inside a while loop. It is the most straightforward approach but also the least efficient since it performs redundant calculations.
Python
a = [145, 375, 100, 2, 10, 40585, 0]
res = [] # initializes an empty list
for num in a:
if num == 0:
continue # 0 is not a strong number
temp = num
sum_fact = 0
while temp > 0:
digit = temp % 10
fact = 1
for i in range(1, digit + 1): # Manual factorial calculation
fact *= i
sum_fact += fact
temp //= 10
if sum_fact == num:
res.append(num)
print(res)
Explanation: For loop iterates through each number in a, skipping 0. For each number, sum_fact stores the sum of its digits' factorials. Using a while loop, it extracts each digit (temp % 10), calculates its factorial in a for loop (fact *= i), adds it to sum_fact and updates temp (temp //= 10). If sum_fact matches the original number, it is added to res.
Similar Reads
Python program to find all Strong Numbers in given list Given a list, write a Python program to find all the Strong numbers in a given list of numbers. A Strong Number is a number that is equal to the sum of factorial of its digits. Examples: Input : [1, 2, 5, 145, 654, 34] Output : [1, 2, 145] Input : [15, 58, 75, 675, 145, 2] Output : [145, 2] Explanat
6 min read
Print all even numbers in a range - Python Our task is to print all even numbers within a given range. The simplest way to achieve this is by using a loop to iterate through the range and check each number for evenness. Let's explore some methods to do so.Using LoopWe can use a for loop with if conditional to check if a number is even.Python
2 min read
Python program to print even numbers in a list Getting even numbers from a list in Python allows you to filter out all numbers that are divisible by 2. For example, given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you might want to extract the even numbers [2, 4, 6, 8, 10]. There are various efficient methods to extract even numbers from a li
3 min read
Print odd numbers in a List - Python We are given a list and our task is to print all the odd numbers from it. This can be done using different methods like a simple loop, list comprehension, or the filter() function. For example, if the input is [1, 2, 3, 4, 5], the output will be [1, 3, 5].Using LoopThe most basic way to print odd nu
2 min read
Print Numbers in an Interval - Python In this article, we are going to learn how to print numbers within a given interval in Python using simple loops, list comprehensions, and step-based ranges.For Example:Input : i = 2, j = 5Output : 2 3 4 5Input : i = 10, j = 20 , s = 2Output : 10 12 14 16 18 20Letâs explore different methods to prin
2 min read