Skip to content

Instantly share code, notes, and snippets.

@akostadinov
Created March 1, 2021 22:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save akostadinov/8bbce37a93573010c82d7707249843a1 to your computer and use it in GitHub Desktop.
amicable numbers
# 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