Skip to content

Instantly share code, notes, and snippets.

@vderyagin
Last active December 15, 2015 15:58
Show Gist options
  • Save vderyagin/5285382 to your computer and use it in GitHub Desktop.
Save vderyagin/5285382 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
class Integer
def factors
fail if self <= 0
(1...self).select { |num| (self % num).zero? }
end
def sum_of_factors
factors.reduce(0, :+)
end
def amicable_pair
s = sum_of_factors
if self < s && s.sum_of_factors == self
return [self, s]
else
return nil
end
end
end
n = 1
loop do
pair = n.amicable_pair
puts "#{pair.first} #{pair[1]}" if pair
n += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment