Python Program to Generate Strong Numbers in an Interval
This python program generates Strong number in an interval given by user.
Strong numbers are those numbers whose sum of factorial of each digits is equal to the original number.
Strong Number Examples, 1 is strong number because 1!=1, 2 is strong number i.e. 2! = 2, 145 is strong number i.e. 1! + 4! + 5! = 1 + 24 + 120 = 145 etc.
Try: Check Strong Number Online & Generate Strong Numbers Online
Python Source Code: Generate Strong Number
# Generate Strong numbers in interval
# function to find factorial
def fact(n):
f = 1
for i in range(1,n+1):
f *= i
return f
# function to check strong
def is_strong(n):
if n< 0:
return False
number_copy = n
strong_sum = 0
while n:
remainder = n%10
strong_sum += fact(remainder)
n //= 10
return strong_sum == number_copy
# Reading interval from user
min_value = int(input('Enter minimum value: '))
max_value = int(input('Enter maximum value: '))
# Looping & displaying if it is Strong
# Here min_vale & max_value are included
print('Strong numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
if is_strong(i):
print(i, end=' ')
Strong Generation: Output
Enter minimum value: -10 Enter maximum value: 100000 Strong numbers from -10 to 100000 are: 0 1 2 145 40585
Strong Generation: Code Explanation
We first read min_value and max_value from user. Function is_strong()
is used to check whether a given number is Strong or not. We loop from min_value
to max_value
and pass each number to is_strong()
function. If this function returns True
, then we print it.