Skip to content

Instantly share code, notes, and snippets.

@clementi
Created March 23, 2011 03:37
Show Gist options
  • Save clementi/882571 to your computer and use it in GitHub Desktop.
Save clementi/882571 to your computer and use it in GitHub Desktop.
Project Euler Problem #34 Solution
def digits(n):
result = []
while n > 0:
result = [n % 10] + result
n /= 10
if not result:
return [0]
return result
def factorial(n):
return 1 if n == 0 else reduce(lambda x, y: x * y, range(1, n + 1))
def digital_factorial_sum(n):
return sum(map(lambda d: factorial(d), digits(n)))
def digital_factorial_sums():
for n in range(1, digital_factorial_sum(9999999) + 1):
if n >= 10 and digital_factorial_sum(n) == n:
yield n
print sum(digital_factorial_sums())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment