#Problem 20
#Sum of digits in factorial
def fact(n):
m = int(n/5)
r = n - (m*5)
i = 1
f = 1
while m > 0:
m = m - 1
f = f * i
i = i + 1
f = f * i
i = i + 1
f = f * i
i = i + 1
f = f * i
i = i + 1
f = f * i
i = i + 1
f = f / 10
while r > 0:
r = r - 1
f = f * i
i = i + 1
return f
def digsum(n):
s = 0
while n > 0:
s = s + (n%10)
n = n/10
return s
if __name__ == "__main__":
n = int(raw_input("Enter n : "))
f = fact(n)
s = digsum(f)
print f, s
Read the code carefully, while it is not perfect (100% correct though), there are many things to learn for newbie.
Friday, September 14, 2012
Sum of digits of factorial of a number
This is another projecteuler.net problem. This is again, very simple and gives programmers a chance to practice any new language they are learning. I wrote it in Python :)
Labels:
Project Euler.net,
Python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment