-
-
Save rizwanreza/bb18a22a26d41f74bdb6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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