Open In App

Python program to print all Strong numbers in given list

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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 =" ")
        


Output

145 2 40585 0 

Time complexity: O(len(num_list)*nlog(n))
Auxiliary Space: O(len(num_list))

Alternative approach: Using Math module and list comprehension.

Python3




#importing math module as m
import math as m
 
# Function to check if a number is strong or not
def is_strong(num):
    sum_1 = sum([m.factorial(int(i)) for i in str(num)])
    return sum_1 == num
 
# initializing list
num_list = [145, 375, 100, 2, 10, 40585, 0]
 
strong_num_list = [num for num in num_list if is_strong(num)]
#printing output
print(strong_num_list)


Output

[145, 2, 40585]

Time complexity: O(n*m), where n is the number of elements in the input list and m is the number of digits in the largest number in the list.
Auxiliary Space: O(n), where n is the number of elements in the input list.

Approach#3: Using filter(): The approach checks whether the numbers in the given list are strong numbers or not using the filter() method and a lambda function. It first defines a factorial() function to calculate the factorial of a number. Then, it defines a list lst containing the numbers to be checked. Finally, it applies the lambda function to the lst using the filter() method to filter the strong numbers from the given list.

  1. Define a factorial() function to calculate the factorial of a number.
  2. Define a list lst containing the numbers to be checked.
  3. Apply the lambda function to the lst using the filter() method to filter the strong numbers from the given list.
  4. Store the filtered numbers in the strong_nums list.
  5. Print the strong_nums list.

Python3




def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)
 
 
lst = [145, 375, 100, 2, 10, 40585, 0]
 
strong_nums = list(filter(lambda num: sum(factorial(int(digit))
                                          for digit in str(num)) == num, lst))
 
print("Strong numbers in the given list are:", strong_nums)


Output

Strong numbers in the given list are: [145, 2, 40585]

Time Complexity: O(nm), n represents the number of elements in the input list, and m represents the maximum number of digits in an element of the input list.
Space Complexity: O(nm)

Approach#4 : Using anonymous function

Define a lambda function to calculate the factorial of a given number. Define another lambda function to check whether a given number is a strong number or not. A number is called a strong number if the sum of the factorial of its digits is equal to the number itself. Create a list of numbers to check for strong numbers. Use the filter function to filter out the strong numbers from the given list of numbers.
Print the list of strong numbers.

Algorithm

1. Define a lambda function for calculating factorial of a number.
2. Define a lambda function for checking if a number is a strong number or not.
3. Initialize a list of numbers to check.
4. Filter out the strong numbers from the list using the filter() function.
5. Print the list of strong numbers.

Python3




factorial = lambda n: 1 if n == 0 else n * factorial(n-1)
is_strong = lambda n: sum(factorial(int(digit)) for digit in str(n)) == n
numbers = [145, 375, 100, 2, 10, 40585, 0]
strong_numbers = list(filter(is_strong, numbers))
print("Strong numbers:", strong_numbers)


Output

Strong numbers: [145, 2, 40585]

Time Complexity: O(n log n), where n is the maximum number in the list. This is because we are iterating over the digits of the numbers and computing their factorials, which takes log(n) time, and we are doing this for each number in the list.

Auxiliary Space: O(1), as we are not using any extra data structure to store the results.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads