Strong Number in Python
A Strong Number is a special number whose sum of the factorials of its digits is equal to the original number itself. For example, the number 145 is a Strong Number because 1!+4!+5!=145.
What is a Strong Number?
A Strong Number or a Krishnamurthy Number is a number for which the sum of the factorials of its digits equals that number itself.
For example, lets take the number 145:
- The digits of 145 are 1, 4, and 5.
- The factorial of 1 is 1!=1.
- The factorial of 4 is 4!=4×3×2×1=24.
- The factorial of 5 is 5!=5×4×3×2×1=120.
Now, sum these factorials:
1!+4!+5!=1+24+120=145
Since the sum of the factorials of the digits (145) is equal to the original number, 145 is a Strong Number.
Step-by-Step Explanation of How to Determine a Strong Number
- Factorial of a Number:
- The factorial of a number nnn is the product of all positive integers less than or equal to n. It is denoted by n!.
- For example:
- 4!=4×3×2×1=24
- 5!=5×4×3×2×1=120
- 0!=1 (by definition)
- Factorial is a must check to see if the number is a Strong Number or not, since factorial of each number in the number has to be found.
2. Extract Digits of the Number:
- To extract the digits of a number you have to convert it to a string. This way you can easily access every single digit.
- For example, if a number is equal to 145, you could transform it to a string like “145.” Then each symbol of the string corresponds to a digit (‘1’, ‘4’, ‘5’).
3. Sum of Factorials of Digits:
- Once you have the digits of the number, you calculate the factorial of each digit and sum them up.
- For 145:
- Factorial of
1
is 1!=1 - Factorial of
4
is 4!=24 - Factorial of
5
is 5!=120 - The sum of these factorials is 1+24+120=145
- Factorial of
4. Comparison:
- Finally, you compare the sum of the factorials of the digits with the original number.
- If the sum is equal to the original number, then it is a Strong Number.
- For 145, 145=145, so it is indeed a Strong Number.
Python Code Explanation
Now let’s take this step-by-step with Python code:
Factorial Function
First of all, you require a function to compute the factorial of a number. You could use Python’s math.factorial()
function, or write your own logic to compute factorial:
def factorial(n):
"""Calculate the factorial of a number."""
if n == 0 or n == 1:
return 1
else:
result = 1
for i in range(1, n + 1):
result *= i
return result
- The
factorial
function, if the argument number is equal to0
or1
, returns value 1-as a definition by convention. - If the number is more than
1
, then it calculates the factorial using afor
loop from1
ton
and then multiply the results.
Checking if a Number is a Strong Number
Next, you need a function to check if a given number is a Strong Number.
def is_strong_number(num):
"""Check if a number is a Strong Number."""
digits = str(num) # Convert the number to a string to extract its digits
sum_of_factorials = sum(factorial(int(digit)) for digit in digits) # Sum of factorials of digits
return sum_of_factorials == num # Check if the sum is equal to the original number
- Converting to a string: We convert the number into a string so that we can iterate over each digit.
- Factorial Calculation: We calculate the factorial of each digit using the
factorial
function. - Sum of Factorials: We make use of Python’s built-in function
sum()
in order to get the sum of factorials of the digits. - Compare: Finally, we compare the sum of the factorials with the number and return
True
if they are equal, which means that the number is a Strong Number.
Testing the Function
You can now test the function with a number:
number = 145
if is_strong_number(number):
print(f"{number} is a Strong Number.")
else:
print(f"{number} is not a Strong Number.")
In this case, the program will print:
145 is a Strong Number.
Optimized Version Using math.factorial
To make the code more concise, you can use Python’s built-in math.factorial()
function instead of writing your own factorial calculation.
import math
def is_strong_number(num):
digits = str(num)
sum_of_factorials = sum(math.factorial(int(digit)) for digit in digits)
return sum_of_factorials == num
number = 145
if is_strong_number(number):
print(f"{number} is a Strong Number.")
else:
print(f"{number} is not a Strong Number.")
math.factorial()
: This function already calculates the factorial for you, which simplifies the code.
Find all strong numbers in a range
If you want to find all Strong Numbers between two numbers, you can create a function that iterates over a range of numbers and checks each one.
import math
def find_strong_numbers(start, end):
strong_numbers = []
for num in range(start, end + 1):
digits = str(num)
sum_of_factorials = sum(math.factorial(int(digit)) for digit in digits)
if sum_of_factorials == num:
strong_numbers.append(num)
return strong_numbers
# Example: Find Strong Numbers between 1 and 500
start, end = 1, 500
print(f"Strong Numbers between {start} and {end}: {find_strong_numbers(start, end)}")
Output for the Range 1 to 500
Strong Numbers between 1 and 500: [1, 2, 145]
- This checks through all the numbers from 1 to 500 to determine which numbers are Strong Number and return their list.
- In this case, the Strong Numbers between 1 and 500 are 1, 2, and 145.
Conclusion
- A strong number is a number whose sum of the factorials of its digits is equal to the number itself.
- We used the Python functions like
str()
for extracting digits,math.factorial()
for the factorial calculations, andsum()
for adding the factorials. - The program can be extended to check a single number or find all Strong Numbers in a given range.