Skip to content

Instantly share code, notes, and snippets.

@mkweick
Created April 9, 2016 18:00
Show Gist options
  • Save mkweick/5bdd88c880f738e4ab42e25c8042d3d7 to your computer and use it in GitHub Desktop.
Save mkweick/5bdd88c880f738e4ab42e25c8042d3d7 to your computer and use it in GitHub Desktop.
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