Created
May 1, 2013 15:53
-
-
Save burtlo/5496133 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 | |
def self.to(input) | |
new(3,5).to(input) | |
end | |
def initialize(*params) | |
@params = params | |
end | |
def to(input) | |
all_multiples = [] | |
@params.each do |param| | |
all_multiples += multiples_upto(param,input) | |
end | |
total(all_multiples.uniq) | |
end | |
def multiples_upto(multiple,input) | |
all_multiples = [] | |
current_multiple = multiple | |
while input > current_multiple | |
all_multiples.push(current_multiple) | |
current_multiple += multiple | |
end | |
all_multiples | |
end | |
def total(multiples) | |
total = 0 | |
multiples.each do |multiple| | |
total += multiple | |
end | |
total | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment