Created
November 27, 2011 17:08
-
-
Save salvianoo/1397835 to your computer and use it in GitHub Desktop.
Project Euler No.1 Ruby
This file contains hidden or 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
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