欧拉计划第一题

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.
求1000以内内被3和5整除的自然数的和

Ruby代码

1
2
3
4
5
6
7
8
9
10
11
12
13
class 
def Multiple3and5Sum
sum = 0
number = 3...1000
number.each do |n|
sum += n if n%3 ==0 ||n%5 ==0
end
puts "The sum is #{sum}"
end
end

el = Euler_2nd.new
el.Multiple3and5Sum()

输出结果:

1
The sum is 233168