Strong Number

A number is called strong number if the number is equal to the sum of factorials of digits of the number.

Example :
Number 145 is a strong number as sum of factorial of 1 , 4 and 5 is equal to 145
145= 1! + 4! + 5!

  • Video Tutorial on Strong Number


There are three parts in this script.
  1. Function to get Factorial of a number
  2. Collecting all digits of the number
  3. Finding sum of the factorial of digits and comparing

Function to get Factorial of a number

We will use one function to get the factorial of any input number.
def my_factorial(n):
  b=1
  for i in range(1,n+1):
    b=b*i
  return b

Digits of an input number

We can get length of a string by using len()
a=input("Enter a number")
for i in range(0,len(a)):
  print(a[i])

Finding sum of the factorials of digit and comparing

Using the factorial function created ( my_factorial() ), the Sum of the factorials of digits of the number can be calculated.
sum=sum+my_factorial(int(a[i]))
Above line is kept inside the for loop. So on each step of the loop one digit of the number is used ( from left to right ).
This value of sum can be compared with the input number by using if condition check and accordingly message can be displayed saying about the strong number.

Full code to check input number is strong number or not

def my_factorial(n):
  b=1
  for i in range(1,n+1):
    b=b*i
  return b
  
a=input("Ener a number : ")
sum=0
for i in range(0,len(a)):
  sum=sum+my_factorial(int(a[i]))
print("Sum of factorial of digits : ",sum)
if int(a)==sum:
  print('This is a strong number : ', a)
else:
  print('This is not a strong number', a)

Listing all strong numbers over a range

We can modify the above code to check all the numbers over a range and display the strong numbers.
def my_factorial(n):
  b=1
  for i in range(1,n+1):
    b=b*i
  return b
  
for a in range(10,100000): # change this range 
  sum=0
  a1=str(a)
  for i in range(0,len(a1)):
    sum=sum+my_factorial(int(a1[i]))
  if a==sum:
    print('This is a strong number : ', a)
Output
This is a strong number :  145
This is a strong number :  40585

All Sample codes Armstrong Number Number equal to cube of its Digits


plus2net.com



Post your comments , suggestion , error , requirements etc here





Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer