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.

     

    Answer:
    40730
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-

    def fac (n ):
    return reduce ( lambda x, y:x * y, [i for i in range ( 1, n + 1 ) ] )
    fact1 = [ 1, 1 ]
    fact2 = [fac (i ) for i in range ( 2, 10 ) ]
    fact1. extend (fact2 )

    def get_up_limit ( ):
    n= 1
    while ( 10 ** n - n * fac ( 9 ) < 0 ):
    n += 1
    return n * fac ( 9 )

    def sum_of_fac (n ):
    s = 0
    while n > 0:
    d = n % 10
    s += fact1 [d ]
    n /= 10
    return s

    n = get_up_limit ( )
    print sum ( i for i in xrange ( 10, n ) if i == sum_of_fac (i ) )