Created
April 9, 2016 18:00
-
-
Save mkweick/5bdd88c880f738e4ab42e25c8042d3d7 to your computer and use it in GitHub Desktop.
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
class SumOfMultiples | |
attr_reader :multiples_of | |
def self.to(max) | |
SumOfMultiples.new(3, 5).to(max) | |
end | |
def initialize(*multiples_of) | |
@multiples_of = multiples_of | |
end | |
def to(max) | |
multiples_sum = 0 | |
(1...max).each { |num| multiples_sum += num if multiple?(num, multiples_of) } | |
multiples_sum | |
end | |
private | |
def multiple?(num, multiples_of) | |
multiples_of.any? { |multiple| num % multiple == 0 } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment