Description
From Project Euler:
n! means n x (n - 1) x ... x 3 x 2 x 1
For example, 10! = 10 x 9 x ... x 3 x 2 x 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!
Solution
The first thing that we need to solve this problem is a quick method for computing factorials. Since Ruby supports arbitrary precision integers, computing 100! will not be too big of a task.
class ::Integer def fact (1..self).reduce(1, :*) end end
We will also need our digits
method from back in Problem 8. With these two things together, we can put together a fairly trivial solution.
100.fact.digits.reduce(0, :+)
The full source and specifications can be seen on github. Next time, evaluating the sum of all amicable pairs under 10,000.
No comments:
Post a Comment