Last active
April 9, 2016 05:23
-
-
Save durrellchamorro/7a923a3c0337987bc585f42e6f7f93fc to your computer and use it in GitHub Desktop.
Sum of Multiples
This file contains 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(*numbers) | |
@numbers = numbers | |
end | |
def self.to(limit) | |
new(3, 5).to(limit) | |
end | |
def to(limit) | |
total_multiples = | |
(1...limit).each_with_object([]) do |possible_multiple, multiples| | |
multiples << possible_multiple if @numbers.any? do |number| | |
possible_multiple % number == 0 | |
end | |
end | |
total_multiples.empty? ? 0 : total_multiples.inject(:+) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment