読者です 読者をやめる 読者になる 読者になる

mu chance or much chance ?

日々の戯れ言

Project Euler 20

Project Euler

Problem20を解き直しました.

  • 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!

https://projecteuler.net/problem=20

問題は「100!の各桁の総和を求めよ」.

num = 1

for i in 2..100 do
	num *= i
end

sum = 0
str = num.to_s
str.length.times do |i|
	sum += str[i].to_i
end

p sum