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