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.
Input : [1, 2, 5, 145, 654, 34] Output : [1, 2, 145] Input : [15, 58, 75, 675, 145, 2] Output : [145, 2]
- We defined 2 functions here: First is
factorial()and second is
- 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, then
factorial()function is called on the remainder of temp mod 10 and passed it to the
- 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 using
append()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:
[1, 2, 145]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.