Tuesday, 28th July, 2009
Project Euler Problem 20
So this is very similar to Problem 16 and infact almost identical code can be used. Maybe I should generalize it?
n! means n × (n − 1) × … × 3 × 2 × 1
Find the sum of the digits in the number 100!
Copy and paste from 16…
Also, as an aside, I’m fairly proud of the little reduce in the middle, Haskell fans are always bragging about how an experienced Haskell coder would write a factorial function as ‘Products [1..n]’ Or some such.
Python is awesome, what with ours being far more general (We can have any binary function, not just products). Undoubtably Haskell has this functionality somewhere, but I haven’t looked yet.
Before you ask; yes, Real World Haskell is on my reading list. Along with about half a dozen other books.
print sum([int(x) for x in str(reduce((lambda x,y:x*y), range(1,101)))])
Done! :-D