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
< ? php function sumofFactors ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ 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 ) { $ sumFactors_n = sumofFactors ( $ n ) ; $ sumFactors_m = sumofFactors ( $ m ) ; $ gcd_n = gcd ( $ n , $ sumFactors_n ) ; $ gcd_m = gcd ( $ m , $ sumFactors_m ) ; if ( $ n / $ gcd_n == $ m / $ gcd_m and $ sumFactors_n / $ gcd_n == $ sumFactors_m / $ gcd_m ) return true ; else return false ; } $ n = 6 ; $ m = 28 ; if ( checkFriendly ( $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >