package main
import (
"fmt"
)
// Example: 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
// Function to compute the factorial of a number
func factorial(n int) int {
fact := 1
for i := 2; i <= n; i++ {
fact *= i
}
return fact
}
// Function to find all numbers equal to the sum of the factorials of their digits
func findDigitFactorialNumbers() {
// Precompute factorials of digits 0-9
factorials := make([]int, 10)
for i := 0; i <= 9; i++ {
factorials[i] = factorial(i)
}
// Define an upper limit (7 * 9! is a safe limit for this problem)
upperLimit := 7 * factorials[9]
// Iterate through all numbers and check the condition
for num := 10; num <= upperLimit; num++ {
sum := 0
temp := num
// Calculate the sum of factorials of digits
for temp > 0 {
digit := temp % 10
sum += factorials[digit]
temp /= 10
}
// Check if the number equals the sum of the factorials of its digits
if sum == num {
fmt.Printf("%d is a digit factorial number.\n", num)
}
}
}
func main() {
findDigitFactorialNumbers()
}
/*
run:
145 is a digit factorial number.
40585 is a digit factorial number.
*/