Last active
December 15, 2015 15:58
-
-
Save vderyagin/5285382 to your computer and use it in GitHub Desktop.
This file contains 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
#! /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