Created
March 1, 2021 22:05
-
-
Save akostadinov/8bbce37a93573010c82d7707249843a1 to your computer and use it in GitHub Desktop.
amicable numbers
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
# https://www.jdoodle.com/execute-ruby-online/ | |
def amicable?(int1, int2) | |
sum_proper_divisors(int1) == int2 && sum_proper_divisors(int2) == int1 | |
end | |
def sum_proper_divisors(num) | |
(1..num/2).select { |i| | |
num%i == 0 | |
}.sum | |
end | |
examples = [ | |
[220, 284], | |
[63020, 76084], | |
[456, 608], | |
] | |
examples.each { |e| | |
puts "Integer #{e[0]} and #{e[1]}: #{amicable?(*e)}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment