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
Project Euler

Solution Project Euler – Problem 6:Sum square difference

# Sum square difference
# The sum of the squares of the first ten natural numbers is,

# 12 + 22 + ... + 102 = 385
# The square of the sum of the first ten natural numbers is,

# (1 + 2 + ... + 10)2 = 552 = 3025
# Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025  385 = 2640.

# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

x = raw_input('Enter your choice for n natural number?: ')
y = int(x)
squaresum = (y*(y+1)*(2*y+1))/6
sum = (y*(y+1)/2)*(y*(y+1)/2)
print sum - squaresum
Project Euler

Solution Project Euler – Problem 20: Factorial digit sum

# Factorial digit sum
# Problem 20
# n! means n (n 1) ... 3 2 1

# For example, 10! = 10 9 ... 3 2 1 = 3628800,
# and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

# Find the sum of the digits in the number 100!
x= raw_input('Enter the number whose factorial you wanna calculate?: ')
y=int(x)
n=1
fact=1
while n < y:
fact=fact*(n+1)
n=n+1
# print fact
sum=0
len = len(str(fact))
strg = str(fact)
for i in range(1,len+1):
# print int(strg[i-1])
sum=sum+int(strg[i-1])
print sum