Jump to content

[PYTHON] Strong numbers finder


Wikkie
 Share

Recommended Posts

Definition of strong number:

  • Strong number is a special number whose sum of the factorial of digits is equal to the original number.
  • For Example: 145 is strong number. Since, 1! + 4! + 5! = 1 + 4*3*2*1 + 5*4*3*2*1 = 145.

Approach: 

  • The idea is to iterate from [1, N] and check if any number between the range is strong number or not. If yes then print the corresponding number, else check for the next number.

 

Function 1 : 

import numpy as np

def get_strong_value(limit): 
  for value in range(1,limit+1):        # iterate from 1 to end limit+1 value
    str_value = str(value)              # converting the int into string, so that we can split the int value into separate numbers (eg. i='145' = i[1] = 4 )
    list_sum = []                       # created an empty list to store the final factorial values
    for i in str_value:                 # looping through the converted string value
      mult=[]                           # created an empty list to store each factorial values
      for j in range(1,int(i)+1):       # here we have converting that string into int, so that we can loop through the range.
        mult.append(j)                  # appending each values into the mult list
      result = np.prod(np.array(mult))  # Here we are using a numpy package to make the multiply easier and making factorial of the whole values in the mult list (eg : if mult list has [4,3,2,1] = 24)
      list_sum.append(result)           # Appending the final factorial values intot the list_sum list
    verify_strong_value(list_sum,value) # Calling verify_strong_value() function and passing the list_sum and vlaues to check whether the current i values is a strong number 

Function 2:

def verify_strong_value(list_sum,value):
    Sum = 0                             #initiated a temp variable
    for item in list_sum:               # looping through the list_sum list to add all the values
      Sum = Sum+item                    # adding the previous value with the next value from the list
    if(Sum == value):                   # checking the whether the sum is equal to the value
      print(f"The given number [{Sum}] is a strong number ")
    else:
      # print("The given number is not a strong number ")
      pass

 

Calling the program:

limit = int(input("Enter a limit : ")) # Getting the end limit value from the user .
get_strong_value(limit)                # initial call of the get_strong_val function

 

Output

Enter a limit : 150
The given number [1] is a strong number 
The given number [2] is a strong number 
The given number [145] is a strong number 

 

Additional source : 

 

I hope it is useful ❤

 

Edited by Wikkie
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.