Skip to content

Instantly share code, notes, and snippets.

@rizwanreza
Forked from johnkpaul/sum_of_factors.rb
Last active August 29, 2015 14:16
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 rizwanreza/bb18a22a26d41f74bdb6 to your computer and use it in GitHub Desktop.
Save rizwanreza/bb18a22a26d41f74bdb6 to your computer and use it in GitHub Desktop.
class SumOfFactors
def initialize(until_num, *divisible_by)
@until_num = until_num
@divisible_by = divisible_by
end
def get_sum
get_range.collect { |num| num if divisible? num }.compact.inject :+
end
private
def divisible?(num)
@divisible_by.any? {|div| num % div == 0 }
end
def get_range
(1..@until_num)
end
end
sum = SumOfFactors.new(100, 3, 5).get_sum
puts sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment