Open In App

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 : 

Method 1:






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

Output
[1, 2, 145]

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()




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

Output
[1, 2, 145]

Method 3: Using list comprehension:

Follow the steps below for implementation:

Below is the implementation of the above approach:




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()

Output
Strong 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:

  1. factorial(n): This function calculates the factorial of a given number using either recursion or a loop.
  2. 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.
  3. 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.
  4. 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.




# 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()

Output
Strong 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.


Article Tags :