Project Euler with Ruby on WSL [Problem 20]

やったぜ 20 だ

NKT...

問題

Factorial digit sum

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!

解答方針

ゴリ押しで。大丈夫。 ruby には bigint がある。 ワンライナー?で解けちゃった。

追伸

よく読んだら問題 100 までは解答公開してもいいのか。そりゃそうか。

def fac(n)
  if n < 2
    1
  else
    n * fac(n-1)
  end
end

fac(100).to_s.split('').map{|d|d.to_i}.sum