Skip to content

Instantly share code, notes, and snippets.

@salvianoo
Created November 27, 2011 17:08
Show Gist options
  • Save salvianoo/1397835 to your computer and use it in GitHub Desktop.
Save salvianoo/1397835 to your computer and use it in GitHub Desktop.
Project Euler No.1 Ruby
def div_by_three_or_five(arry)
sum = 0
arry.each do |item|
if item % 3 == 0 or item % 5 == 0
sum += item
end
end
sum
end
def div_by_three_or_five(arry)
sum = 0
arry.each { |item| sum += item if item % 3 == 0 or item % 5 == 0 }
sum
end
def div_by_three_or_five(arry)
sum = 0
cond = proc {|el| el % 3 == 0 or el % 5 == 0}
arry.each {|el| sum += el if cond.call(el) }
sum
end
def div_by_three_or_five(arry)
cond = proc {|el| el % 3 == 0 or el % 5 == 0}
arry.select {|el| cond.call(el)}.inject {|sum, el| sum + el}
end
div_by_three_or_five (1..1000).to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment