145 is a curious number, as \$1! + 4! + 5! = 1 + 24 + 120 = 145\$.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as \$1! = 1\$ and \$2! = 2\$ are not sums they are not included.
I can't figure out a fair way to optimize the upper bound from the information given in the question. I went on the PE forum and found many people setting the upper bound to 50000 because they knew that would be large enough after testing. This doesn't seem fair to me; I want to set the bound based on the information in the question. Right now it runs in around 20 seconds.
EDIT: I'm not looking for a mathematical algorithm. I'm looking for ways to make this code faster.
from math import factorial as fact
from timeit import default_timer as timer
start = timer()
def findFactorialSum():
factorials = [fact(x) for x in range(0, 10)] # pre-calculate products
total_sum = 0
for k in range(10, fact(9) * 7): # 9999999 is way more than its fact-sum
if sum([factorials[int(x)] for x in str(k)]) == k:
total_sum += k
return total_sum
ans = findFactorialSum()
elapsed_time = (timer() - start) * 1000 # s --> ms
print "Found %d in %r ms." % (ans, elapsed_time)