Not cool: Summing factorials of digits

What is the sum of the first four non-negative numbers with the following property: the number is equal to the sum of the factorials of its digits.

For instance, 23 is NOT such a number, since 2!+3! = 2 + 6 = 8, which is not equal to 23.


The answer is 40733.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Bill Bell
Jan 24, 2015

I did exactly the same thing, except I put the factorials of 0 through 9 in a hash table so it didn't have to calculate factorials over and over again.

Python 2.7:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from math import factorial
fac = {}
for n in xrange(10):
    fac[str(n)] = factorial(n)
def goal(n):
    fac_sum = sum([fac[c] for c in str(n)])
    return n == fac_sum
found = 0
total = 0
n = 0
while found < 4:
    if goal(n):
        found += 1
        total += n
    n += 1
print "Answer:", total

Brock Brown - 6 years, 2 months ago

Log in to reply

That's considered good practice, although the numbers involved here are small. (I was too lazy!) Is it possible that you are unaware of memoisation for decorating a function so that its results are saved in case they might be needed again? Here's how I might have used this programming technique for this problem.

Bill Bell - 6 years, 1 month ago

Solution in Python:

def factorial(n): # returns the factorial of that number.
     if (n == 0):
         return 1
     else:
          factorial = 1
          for i in range(1,n+1):
                  factorial *= i
          return factorial

factorial_digits = {}
for i in range(0,10):
    factorial_digits[i] = factorial(i) 
# factorial_digits contains the factorial value for each digit with its original digit as a key

def corresponds_to_sum_of_factorial_digits(n):
    digits = list(str(n))
    summed = 0
    for i in digits:
         summed += factorial_digits[int(i)]
    if (summed == n):
         return True
    return False
    # checks wheter or not the condition specified on the exercise is met.

final_numbers = [] #numbers which follow the condition specified.

# the sum of the factorials from 0 to 9 equals 409114 which has 6 digits, therefore, 6 nested 'for' loops are the maximum needed to solve the problem.

for i in range(0,10): 
    for f in range(0,10):
        for z in range(0,10):
           for g in range(0,10):
               for p in range(0,10):
                   for h in range(0,10):
                       number = int(str(i) + str(f) + str(z) + str(g) + str(p) + str(h))
                       if corresponds_to_sum_of_factorial_digits(number):
                           final_numbers.append(number)
    if (len(final_numbers) == 4):
        break

final_sum = 0

for n in final_numbers:
    final_sum += n

#final_numbers == [1,2,145,40585]
#final_sum == 40733

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...