class Integer def divisors divisors = [] (1..Math.sqrt(self).to_i).each do |num| if self % num == 0 divisors << num divisors << self / num end end divisors end end triangle = Enumerator.new do |y| sum = 1 next_num = 2 loop do y << sum sum += next_num next_num += 1 end end val = triangle.next while val.divisors.count < 500 val = triangle.next end puts val