Skip to content

Instantly share code, notes, and snippets.

@johnkpaul
Created March 7, 2015 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnkpaul/6e183c0a027505d2fcaf to your computer and use it in GitHub Desktop.
Save johnkpaul/6e183c0a027505d2fcaf 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.select(&self.method(:is_disivible_by))
.inject(&self.method(:sum_arr))
end
private
def sum_arr(sum, num)
sum + num
end
def is_disivible_by num
@divisible_by.any? {|divisible_by| num % divisible_by == 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