Python program to print all Strong numbers in given list


A number is said to be a Strong number if the factorials of its individual digits are found and then added together and the total sum calculated is equal to the number itself. Here, in this Python article, using three different examples, the different methods of finding the Strong numbers if present in a list are given. In the Example 1, a list that contains the precalculated factorials of numbers are used for calculating the sum of factorials of all the present digits in a number while checking whether the selected number is strong or not. In example 2, the factorials of all numbers from 0 to 9 are calculated using a function and stored in a list and then this list is used for calculating the sum of factorials of all digit in a number while checking if the number of strong or not. In example 3, the digits from the number being checked are separated and their factorial is calculated and added together, without using a separate factorial list.

Example 1 - Print all Strong numbers in given list using a separate list of precalculated factorials of individual digits, in the process

Algorithm

Step 1 âˆ’ Get a list of integers. Make sure that this test list has at least one Strong number. Make a function called numType to check if the number is strong or not.

Step 2 âˆ’ Make a separate list of pre-calculated factorials of numbers from 0 to 9.

Step 3 âˆ’ For each number from the test list, using the function numType, first separate the digits of that number, and pick the precalculated factorial from the factorial list.

Step 4 âˆ’ Add the sum of these factorials together. If the number is equal to the total sum, print that number.

Step 5 âˆ’ Run the program and then check the result.

The Python File Contains this

list1=[300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
print("Sample List: ", list1)
#List having listoffactorials of numbers from 1 to 9
listoffactorials = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

def numType(num):
   numstr = str(num)
   sum=0

   for digit in numstr:
      sum += listoffactorials[int(digit)]

   if sum == num:
      return "isStrong"
   else:
      return "notStrong"    


for item in list1:
   if (numType(item) == "isStrong"):
      print("Strong Number: ", item)  

Viewing The Result

For seeing the result run the Python file in the cmd window.

Sample List:  [300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
Strong Number:  40585
Strong Number:  2
Strong Number:  1
Strong Number:  145

Example 2: Print all Strong numbers in a given list by calculating the factorials of individual digits using a function and making their separate list, in the process

Algorithm

Step 1 âˆ’ Specify a list of integers. Make sure that this sample list has at least one Strong number. Make a function called numType to check if the number is strong or not.

Step 2 âˆ’  Make a function calcFactorial() and call it for numbers 0 to 9 and create a separate list of factorials of numbers from 0 to 9.

Step 3 âˆ’ For each number from the test list, using the function numType, first separate the digits of that number, and pick the calculated factorial from the factorial list. Use the string type of number form for separating digits and then change these back to integers.

Step 4 âˆ’ Add the sum of these factorials together. If the number is equal to the total sum, print that Strong number.

Step 5 âˆ’ Run the program and then check the result.

The Python File Contains this

listoffactorials=[]

def calcFactorial(num):
   factorial=1
   if num < 0:
      print("Error, the number is negative")
   elif num > 0:
      for item in range(1 , num + 1):
         factorial = factorial*item
      return  factorial  
   else:
      return 1
   
for item in range(0, 10):
   listoffactorials.append(calcFactorial(item))

print("Calculated digits factorial list:", listoffactorials)    

def numType(num):
   numstr = str(num)
   sum=0

   for digit in numstr:
      sum += listoffactorials[int(digit)]

   if sum == num:
      return "isStrong"
   else:
      return "notStrong"    
 
list1=[300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
print("Sample List: ", list1)
for item in list1:
   if (numType(item) == "isStrong"):
      print("Strong Number: ", item)

Viewing The Result

Open the cmd window and run the python file to see the result.

Calculated digits factorial list: [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
Sample List:  [300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
Strong Number:  40585
Strong Number:  2
Strong Number:  1
Strong Number:  145   

Example 3: Print all Strong numbers in given list without using a separate list of factorials of individual digits, in the process

Algorithm

Step 1 âˆ’ Make a list of integers. Make sure that this sample list has at least one Strong number.

Step 2 âˆ’ Make a function calcFactorial() to calculate factorials of a specified number.

Step 3 âˆ’ For each number from the test list, first separate the digits of that number, and calculated its factorial. Then add this factorial to the sum. Use the integer type of number form for separating digits by dividing by ten and checking the remainder.

Step 4 âˆ’ Check if the number is equal to the total sum and then print that Strong number.

Step 5 âˆ’ Run the program and then check the result.

The Python File Contains this

list1=[300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
print("Sample List: ", list1)
def calcFactorial(num):
   factorial=1
   if num < 0:
      print("Error, the number is negative")
   elif num > 0:
      for item in range(1 , num + 1):
         factorial = factorial*item
      return  factorial  
   else:
      return 1

for item in list1:
   tempItem = item
   total = 0
   while(tempItem):
      rem = tempItem % 10
      total += calcFactorial(rem)
      tempItem = tempItem // 10
   if(total == item):
      print("Strong Number: ", item)
   else:
      print("Not a Strong Number: ", item)

Viewing The Result

Open the cmd window and run the python file to see the result.

Sample List:  [300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
Not a Strong Number:  300
Strong Number:  40585
Not a Strong Number:  500
Not a Strong Number:  460
Not a Strong Number:  3
Strong Number:  2
Not a Strong Number:  89
Strong Number:  1
Strong Number:  145
Not a Strong Number:  260
Not a Strong Number:  630 

In this Python article, using three different examples, the methods of finding the Strong numbers from a given list of integers, are given. In example 1, the method uses a pre-calculated list of factorials for calculating the factorials of individual digits and then their sum. In example 2, instead of using the precalculated list, the list of factorials is made by using a function for calculating factorials. In both examples, 1 and 2, the string form of the number is used to find individual digits. In example 3, the digits are separated by using the divide and find a remainder method, then their factorials are calculated and added together.

Updated on: 08-Aug-2023

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements