You are here: Home Programming Python Project Euler 020 - sum factorial digits

020 - sum factorial digits

Find the sum of the digits in the number 100!

See Problem 20.

This rescues a lot of code from previous problems. To summarise, we borrow the factorial function from problem 15 to calculate the number. We then transform it to a string and then a list of ints so as to get the digits. The quick sum operator is borrowed from Python Idioms.

def fact(x): return (1 if x==0 else x * fact(x-1))

fact_num = fact (100)
num_str = map (int, str (fact_num))

from operator import add

sum = reduce (add, num_str)

print sum

The answer is 648. Computation time is negligible.

Document Actions
Visitors
Locations of visitors to this page
 
Personal tools