Created
July 1, 2012 22:26
-
-
Save pjc/3029856 to your computer and use it in GitHub Desktop.
Euler Project in Ruby - Problem 1
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
y = 0 | |
(1..999).each { |x| y = y + x if (x % 3 == 0) || (x % 5 == 0) } | |
puts y |
def multiple_of_three?(n)
n % 3 == 0
end
def multiple_of_five?(n)
n % 5 == 0
end
def acceptable(n)
multiple_of_three?(n) || multiple_of_five?(n)
end
p (0..999).select { |n| acceptable(n) }.reduce(:+)
Here is my code sum = (1...1000).select {|x| x if x % 3 == 0 || x % 5 == 0}.reduce(:+)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(1..999).select{|x| (x % 3 == 0) || (x % 5 == 0) }.reduce(:+)