sum_of_multiples.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class SumOfMultiples
  def initialize(*factors)
    @factors = factors
  end

  def to(n)
    self.class.to(n, @factors)
  end

  def self.to(n, factors=[3, 5])
    multiples = (1...n).select do |i|
      factors.map { |f| i % f == 0 }.any?
    end
    multiples.inject(0) {|sum, x| sum + x}
  end
end

Comments


You're not logged in right now. Please login via GitHub to comment