Amicable Pairs | Sololearn: Learn to code for FREE!

+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)

7/1/2017 10:51:13 AM

Salekin

3 Answers

New Answer

+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.

+1

https://code.sololearn.com/c7JanWTaQtoW/?ref=app

0

@Igor B, Wow, Good catch man, thanks :) I've updated the code. PS: can anybody please tell me how to reply to comments?