Project Euler

Solution Project Euler – Problem 34: Digit Factorials

# 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.

def factorial(x):
  y=int(x)
  n=1
  fact=1
  while n < y:
    fact=fact*(n+1)
    n=n+1
  return fact

sum1 = 0
for j in range(10,100000):
  str1 = str(j)
  length = len(str1)
  sum = 0
  for i in range(1,length+1):
    sum = sum + factorial(str1[i-1])
  if sum == j:
    sum1 = sum1 + sum
print sum1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s