Python program to find all Strong Numbers in given list
Last Updated :
17 Apr, 2023
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]
Explanation :
- We defined 2 functions here: First is factorial() and second is strong_number().
- As soon as strong_number() is called, the list is passed to the function and stored in the formal argument list.
- For loop iterates for every element in the list, temp is a temporary variable on which calculation is done, then the factorial() function is called on the remainder of temp mod 10 and passed to the factorial function.
- Now when temp equates to 0, it exits the while loop and checks whether the sum is equal to x or not. If True then it is added to the list using the append() function which is predefined for the list and is used to add elements to the list if there is no strong number then it will return an empty list.
Method 1:
Python3
# Python program to find all
# Strong Numbers in given list
def factorial(number):
if(number == 0 or number == 1):
fact = 1
else:
fact = number * factorial(number - 1)
return fact
def strong_number(list):
new_list = []
for x in list:
temp = x
sum = 0
while(temp):
rem = temp % 10
sum += factorial(rem)
temp = temp // 10
if(sum == x):
new_list.append(x)
else:
pass
return new_list
# Driver Code
val_list = [1, 2, 5, 145, 654, 34]
strong_num_list = strong_number(val_list)
print(strong_num_list)
Time Complexity: O(n*n), where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list
Method 2 : Using math.factorial()
Python3
# Python program to find all
# Strong Numbers in given list
def factorial(number):
import math
return math.factorial(number)
def strong_number(list):
new_list = []
for x in list:
temp = x
sum = 0
while(temp):
rem = temp % 10
sum += factorial(rem)
temp = temp // 10
if(sum == x):
new_list.append(x)
else:
pass
return new_list
# Driver Code
val_list = [1, 2, 5, 145, 654, 34]
strong_num_list = strong_number(val_list)
print(strong_num_list)
Method 3: Using list comprehension:
Follow the steps below for implementation:
- Define a function factorial(n) to calculate the factorial of a number n. The factorial of a number is the product of all the positive integers less than or equal to n.
- Define a function is_strong_number(number) to check if a number is a strong number or not. To check if a number is a strong number, calculate the sum of factorials of each digit of the number. If the sum is equal to the original number, then it is a strong number.
- Define a function find_strong_numbers(numbers) to find strong numbers in a given list of numbers. This function uses the is_strong_number function to check if a number is a strong number or not. If it is a strong number, it is added to the list of strong numbers.
- Call the find_strong_numbers function with the list of numbers as an argument and print the result.
Below is the implementation of the above approach:
Python3
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def is_strong_number(number):
original_number = number
sum_of_factorials = sum([factorial(int(digit)) for digit in str(number)])
if sum_of_factorials == original_number:
return True
else:
return False
def find_strong_numbers(numbers):
return [number for number in numbers if is_strong_number(number)]
def main():
numbers = [1, 2, 5, 145, 654, 34]
strong_numbers = find_strong_numbers(numbers)
print("Strong numbers in the given list:", strong_numbers)
if __name__ == '__main__':
main()
OutputStrong numbers in the given list: [1, 2, 145]
Time complexity: O(d * n) where d is the number of digits in the largest number in the list and n is the number of elements in the list.
Auxiliary space: O(n), as we are using a list to store the strong numbers. The size of the list depends on the number of strong numbers in the given list, which is at most n.
Method 4: Using loop
The program includes four functions:
- factorial(n): This function calculates the factorial of a given number using either recursion or a loop.
- is_strong_number(number): This function checks whether a given number is a strong number or not by calculating the sum of factorials of digits in the number.
- find_strong_numbers(numbers): This function takes a list of numbers as input and returns a list of all the strong numbers in that list by filtering the input list using the is_strong_number() function.
- main(): This function is the driver code that creates a list of numbers and calls the find_strong_numbers() function to find all the strong numbers in the list, and then prints the result to the console.
Python3
# function to calculate factorial using a loop
def factorial(n):
result = 1
# iterate from 2 to n and multiply the result by the current number
for i in range(2, n+1):
result *= i
return result
# function to check if a number is a strong number
def is_strong_number(number):
original_number = number
# calculate the sum of factorials of digits in the number
sum_of_factorials = sum([factorial(int(digit)) for digit in str(number)])
# check if the sum of factorials is equal to the original number
if sum_of_factorials == original_number:
return True
else:
return False
# function to find all the strong numbers in a list of numbers
def find_strong_numbers(numbers):
# use list comprehension to filter the numbers that are strong
return [number for number in numbers if is_strong_number(number)]
# driver code
def main():
numbers = [1, 2, 5, 145, 654, 34]
strong_numbers = find_strong_numbers(numbers)
print("Strong numbers in the given list:", strong_numbers)
if __name__ == '__main__':
main()
OutputStrong numbers in the given list: [1, 2, 145]
Time complexity: O(d * n) where d is the number of digits in the largest number in the list and n is the number of elements in the list.
Auxiliary space: O(n), as we are using a list to store the strong numbers. The size of the list depends on the number of strong numbers in the given list, which is at most n.
Similar Reads
Print all Strong Numbers in Given List - Python 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,
3 min read
Python program to find all possible pairs with given sum Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K. Examples: Input : lst =[1, 5, 3, 7, 9] K = 12 Output : [(5, 7), (3, 9)] Input : lst = [2, 1, 5, 7, -1, 4] K = 6 Output : [(2, 4), (1, 5), (7, -1)] Method #1: Pythonic Naive This
7 min read
Python | Find groups of strictly increasing numbers in a list Given a list of integers, write a Python program to find groups of strictly increasing numbers. Examples: Input : [1, 2, 3, 5, 6] Output : [[1, 2, 3], [5, 6]] Input : [8, 9, 10, 7, 8, 1, 2, 3] Output : [[8, 9, 10], [7, 8], [1, 2, 3]] Approach #1 : Pythonic naive This is a naive approach which uses a
5 min read
Python program to count positive and negative numbers in a list In this article, we will explore various methods to count positive and negative numbers in a list. The simplest way to do this is by using a loop. This method counts the positive and negative numbers in a list by iterating through each element using for loop.Pythona = [10, -20, 30, -40, 50, -60, 0]
2 min read
Python Program to Print all Prime numbers in an Interval The task of printing all prime numbers in an interval in Python involves taking two input values representing a range [x, y] and finding all prime numbers within that range. A prime number is a natural number greater than 1 that is divisible only by 1 and itself. For example, in the interval [2, 7],
4 min read