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]
Explanation :
- We defined 2 functions here: First is
factorial()and second isstrong_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 list,
tempis a temporary variable on which calculation is done, thenfactorial()function is called on the remainder of temp mod 10 and passed it to thefactorialfunction. - Now when
tempequates to 0, it exits the while loop and checks whether sum is equal to x or not. If True then it is added in the list usingappend()function which is predefined for list and is used to add elements in the list and if there is no strong number then it will return an empty list.
Below is the Python implementation:
# 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) |
chevron_right
filter_none
Output:
[1, 2, 145]
Recommended Posts:
- Python program to print all Strong numbers in given list
- Python program to print even numbers in a list
- Python program to count Even and Odd numbers in a List
- Python program to print odd numbers in a List
- Python program to print positive numbers in a list
- Python program to print negative numbers in a list
- Python program to count positive and negative numbers in a list
- Python | Find groups of strictly increasing numbers in a list
- Python | Find missing numbers in a sorted list range
- Python program to find Cumulative sum of a list
- Python program to find sum of elements in list
- Python program to find second largest number in a list
- Python program to find smallest number in a list
- Python program to find largest number in a list
- Python program to find N largest elements from a list
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.


