# Euler Problem No. 1 # 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. #function that determines if a number is a multiple of 3 or 5 def div_by_3_or_5 (num) if num%3==0 || num%5==0 then num else 0 end end #naturals is an array that holds all natural numbers from 1 to 999 that are multiples of 3 or 5 naturals = Array.new for x in 1...1000 # . . . denotes not inclusive of the upper bound if div_by_3_or_5(x) != 0 then naturals << x end end #sum up the numbers in the array, then output the sum ret = naturals.inject(0) {|x,n| x+n} print "#{ret} is the sum \n" #another way is to simply add the values, it's just not as fun ret2 = 0 for y in 1...1000 ret2 = ret2 + div_by_3_or_5(y) end print "#{ret2} is the sum\n"