Open In App

Python program to find all Strong Numbers in given list

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 is strong_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 the list, temp is a temporary variable on which calculation is done, then the factorial() function is called on the remainder of temp mod 10 and passed to the factorial function.
  • Now when temp equates to 0, it exits the while loop and checks whether the sum is equal to x or not. If True then it is added to the list using the append() function which is predefined for the list and is used to add elements to the list if there is no strong number then it will return an empty list.

Method 1:

Python3




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

Python3




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

  • Define a function factorial(n) to calculate the factorial of a number n. The factorial of a number is the product of all the positive integers less than or equal to n.
  • Define a function is_strong_number(number) to check if a number is a strong number or not. To check if a number is a strong number, calculate the sum of factorials of each digit of the number. If the sum is equal to the original number, then it is a strong number.
  • Define a function find_strong_numbers(numbers) to find strong numbers in a given list of numbers. This function uses the is_strong_number function to check if a number is a strong number or not. If it is a strong number, it is added to the list of strong numbers.
  • Call the find_strong_numbers function with the list of numbers as an argument and print the result.

Below is the implementation of the above approach:

Python3




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.

Python3




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads