Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save steveklabnik/5496167 to your computer and use it in GitHub Desktop.
Save steveklabnik/5496167 to your computer and use it in GitHub Desktop.
class SumOfMultiples
def initialize(*ints)
@numbers = ints.to_a
end
def self.to(max)
new(3, 5).to(max)
end
def to(max)
multiples = collect_multiples_upto(max)
multiples.inject(:+)
end
def collect_multiples_upto(max)
multiples = []
max.times {|int| multiples << int if is_divisor_of?(int)}
multiples
end
def is_divisor_of?(int)
@numbers.any? {|num| int % num == 0 }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment