Rubyで3と5の倍数を求める 〜Rubyでオイラープロジェクトを解こう!Problem1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
10未満の自然数で3または5の倍数であるものは、3、5、6および9であり、それらの合計は23である。
では1,000未満で3または5の倍数であるものの合計はいくらか。
Project Eulerというものがあることを知った
刺激されてやってみた
def multi_of_three_or_five_upto(max) sum = 0 1.upto(max-1) do |n| sum += n if (n % 3).zero? or (n % 5).zero? end sum end multi_of_three_or_five_upto 1000 # => 233168