Check if the given two numbers are friendly pair or not | Returns sum of all factors of n . ; Traversing through all prime factors . ; THE BELOW STATEMENT MAKES IT BETTER THAN ABOVE METHOD AS WE REDUCE VALUE OF n . ; This condition is to handle the case when n is a prime number greater than 2. ; Function to return gcd of a and b ; Function to check if the given two number are friendly pair or not . ; Finding the sum of factors of n and m ; finding gcd of n and sum of its factors . ; finding gcd of m and sum of its factors . ; checking is numerator and denominator of abundancy index of both number are equal or not . ; Driver Code
function sumofFactors ( n ) { let res = 1 ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { let count = 0 , curr_sum = 1 ; let curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function checkFriendly ( n , m ) { let sumFactors_n = sumofFactors ( n ) ; let sumFactors_m = sumofFactors ( m ) ; let gcd_n = gcd ( n , sumFactors_n ) ; let gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } let n = 6 , m = 28 ; if ( checkFriendly ( n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;