Count all pairs of divisors of a number N whose sum is coprime with N | Function to calculate GCD ; Function to count all valid pairs ; Initialize count ; Check if sum of pair and n are coprime ; Return the result ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } function CountPairs ( n ) { let cnt = 0 ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { let div1 = i ; let div2 = Math . floor ( n / i ) ; let sum = div1 + div2 ; if ( gcd ( sum , n ) == 1 ) cnt += 1 ; } } return cnt ; } let n = 24 ; document . write ( CountPairs ( n ) + " " ) ;