Print all numbers that are divisors of N and are co | JavaScript program for tha above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function printUnitaryDivisors ( n ) { for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && gcd ( i , n / i ) == 1 ) { document . write ( i + " " ) ; } else { if ( gcd ( i , n / i ) == 1 ) { document . write ( i + " " + n / i + " " ) ; } } } } } var N = 12 ; printUnitaryDivisors ( N ) ;