fork(2) download
  1. from math import factorial as fact
  2. from timeit import default_timer as timer
  3.  
  4. start = timer()
  5.  
  6. def findFactorialSum():
  7. factorials = [fact(x) for x in range(0, 10)] # pre-calculate products
  8. total_sum = 0
  9. for k in range(10, fact(9) * 7): # 9999999 is way more than its fact-sum
  10. tmp = k
  11. total = 0
  12. while tmp > 0:
  13. total += factorials[tmp % 10]
  14. tmp //= 10
  15.  
  16. if total == k:
  17. total_sum += k
  18.  
  19. return total_sum
  20.  
  21. ans = findFactorialSum()
  22. elapsed_time = (timer() - start) * 1000 # s --> ms
  23.  
  24. print "Found %d in %r ms." % (ans, elapsed_time)
  25.  
Success #stdin #stdout 4.6s 8208KB
stdin
Standard input is empty
stdout
Found 40730 in 4592.034816741943 ms.