+ 2

Amicable Pairs

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. - wikipedia. So, 220 and 284 are amicable pairs, because, proper divisors of 220 are [1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110] and sum of 220's proper divisors are 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284 Again, proper divisors of 284 are [1, 2, 4, 71, 142] and sum of it's proper divisors are 1 + 2 + 4 + 71 + 142 = 220 I've written a code to check if given pair are amicable or not. Please comment about how to optimize my code or to make it better. Thank you. https://code.sololearn.com/cQS73oi9YeyJ PS: Insert your numbers as comma separated. Like '220, 284' Also, the first 10 amicable pair numbers are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992)

1st Jul 2017, 10:51 AM
Salekin
Salekin - avatar
3 odpowiedzi
+ 1
Minor point. I'm not familiar with Ruby, but this expression: sum_of_a_divisors == b && sum_of_b_divisors == a is already a Boolean. So, you probably don't need a ternary "? true : false" after it.
30th Jun 2017, 1:12 PM
Igor B
Igor B - avatar
22nd Feb 2018, 1:56 PM
Abhimanyu Gupta
Abhimanyu Gupta - avatar
0
@Igor B, Wow, Good catch man, thanks :) I've updated the code. PS: can anybody please tell me how to reply to comments?
30th Jun 2017, 2:23 PM
Salekin
Salekin - avatar