require 'prime'
class Integer
    def my_divisor_list2
        return [1] if self == 1
        Prime.prime_division(self).map do |e|
        Array.new(e[1]+1).map.with_index do |element, i|
            e[0]**i
        end
        end.inject{|p,q| p.product(q)}.map do |a|
        [a].flatten.inject(:*)
        end.sort
    end
end

divisors = 120.my_divisor_list2 #=> [1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120]
distances = []
while divisors.size != 0
    distances.push(divisors.shift + divisors.pop - 2)
end

p distances.min