Created
May 24, 2012 02:28
-
-
Save coryschires/2779085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
first = 0 | |
last = 1000 | |
total = 0 | |
multipules = [] | |
# build the array of multipules | |
(first..last).each do |list| | |
if (list % 5 == 0) or (list % 3 == 0) | |
multipules.push(list) | |
end | |
end | |
# add them up | |
multipules.each do |multipule| | |
total += multipule | |
end | |
puts "Total is: #{total}" | |
# ======================================== | |
# Or make your own `sum` method on array | |
class Array | |
def sum | |
total = 0 | |
self.each { |number| total += number } | |
total | |
end | |
end | |
puts "Total is: #{multipules.sum}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment