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