Skip to content

Instantly share code, notes, and snippets.

@coryschires
Created May 24, 2012 02:28
Show Gist options
  • Save coryschires/2779085 to your computer and use it in GitHub Desktop.
Save coryschires/2779085 to your computer and use it in GitHub Desktop.
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