Check for Amicable Pair | Function to calculate sum of all proper divisors of a given number ; Sum of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Returns true if x and y are Amicable else false . ; Driver Code
function divSum ( n ) { let result = 0 ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + 1 ) ; } function areAmicable ( x , y ) { if ( divSum ( x ) != y ) return false ; return ( divSum ( y ) == x ) ; } let x = 220 ; let y = 284 ; if ( areAmicable ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;