Count numbers having GCD with N equal to the number itself | Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code
function countNumbers ( N ) { var count = 0 ; for ( i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { count ++ ; if ( parseInt ( N / i ) != i ) { count ++ ; } } } return count ; } var N = 10 ; document . write ( countNumbers ( N ) ) ;