Skip to content

Instantly share code, notes, and snippets.

@durrellchamorro
Last active April 9, 2016 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durrellchamorro/7a923a3c0337987bc585f42e6f7f93fc to your computer and use it in GitHub Desktop.
Save durrellchamorro/7a923a3c0337987bc585f42e6f7f93fc to your computer and use it in GitHub Desktop.
Sum of Multiples
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