This Python program checks all three-digit numbers (from 100 to 999).
It prints those numbers where the sum of the factorials of their digits equals the number itself.
For example, 145 is valid because 1! + 4! + 5! = 145.
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
print("Three-digit numbers where the sum of digit factorials equals the number:")
for num in range(100, 1000):
digits = [int(d) for d in str(num)]
total = sum(factorial(d) for d in digits)
if total == num:
print(num)
Three-digit numbers where the sum of digit factorials equals the number:
145
Here’s how the program works:
- The factorial() function calculates the factorial of each digit
- Each number is split into its digits using string conversion
- The sum of the digit factorials is compared to the original number
- If they match, the number is printed