Skip to content

Instantly share code, notes, and snippets.

@johnkpaul
Created March 7, 2015 17:22

Revisions

  1. johnkpaul created this gist Mar 7, 2015.
    32 changes: 32 additions & 0 deletions sum_of_factors.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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