Rubyで階乗して桁を合計 〜Rubyでオイラープロジェクトを解こう!Problem20
n! means n × (n - 1)× ... × 3 × 2 × 1
Find the sum of the digits in the number 100!
n!は n × (n - 1)× ... × 3 × 2 × 1を意味する。
100!における桁の合計を求めよ。
Integerクラスのインスタンスメソッドとして
!とsum_digitを定義してみた
class Integer def ! (1..self).inject(:*) end def sum_digit n = self.abs sum = 0 until n <= 0 a, b = n.divmod(10) sum += b n = a end sum end end 100.!.sum_digit # => 6xx