Skip to content

Instantly share code, notes, and snippets.

@gschorkopf
Last active December 16, 2015 20:59
Show Gist options
  • Save gschorkopf/5496101 to your computer and use it in GitHub Desktop.
Save gschorkopf/5496101 to your computer and use it in GitHub Desktop.
SumOfMultiples warmup solution
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