Python program to print all Strong numbers in given list
Given a list, write a Python program to print all the strong numbers in that list.
Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number.
Example for checking if number is Strong Number or not.
Input: n = 145 Output: Yes Explanation: Sum of digit factorials = 1! + 4! + 5! = 1 + 24 + 120 = 145
Steps for checking number is strong or not :
1) Initialize sum of factorials as 0. 2) For every digit d, do following a) Add d! to sum of factorials. 3) If sum factorials is same as given number, return true. 4) Else return false.
Let’s see the Python program for this problem :
# Python3 program to print # all strong numbers in a list. # Define a function for calculating # factorial of a number def factorial(number): fact = 1 if number = = 0 or number = = 1 : return fact for i in range ( 2 , number + 1 ) : fact * = i return fact # Define a function for checking a # number is strong number or not def find_strong_numbers(num_list): result = [] # loop till list is not empty for num in num_list : sum = 0 temp = num # loop till number is not zero while num ! = 0 : r = num % 10 # function call sum + = factorial(r) num / / = 10 # check number is strong or not if sum = = temp: # adding number to the list result.append(temp) # return list of strong numbers return result # Driver Code if __name__ = = "__main__" : num_list = [ 145 , 375 , 100 , 2 , 10 , 40585 , 0 ] # function call strong_num_list = find_strong_numbers(num_list) # loop till list is not empty for strong_num in strong_num_list : print (strong_num, end = " " ) |
chevron_right
filter_none
Output:
145 2 40585 0
Recommended Posts:
- Python program to find all Strong Numbers in given list
- Python program to print even numbers in a list
- Python program to print odd numbers in a List
- Python program to print negative numbers in a list
- Python program to print positive numbers in a list
- Python | Program to print duplicates from a list of integers
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- Python Program to Print Numbers in an Interval
- Python program to print all negative numbers in a range
- Python program to print all Prime numbers in an Interval
- Python program to print all positive numbers in a range
- Python program to count Even and Odd numbers in a List
- Python program to count positive and negative numbers in a list
- Generating Strong Password using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.