Count all possible values of K less than Y such that GCD ( X , Y ) = GCD ( X + K , Y ) | Function to find the gcd of a and b ; Function to find the number of Ks ; Find gcd ; Calculating value of totient function for n ; Given X and Y
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function calculateK ( x , y ) { var g = gcd ( x , y ) ; var n = y / g ; var res = n ; for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { res -= ( res / i ) ; while ( n % i == 0 ) n /= i ; } } if ( n != 1 ) res -= ( res / n ) ; return res ; } var x = 3 , y = 15 ; document . write ( calculateK ( x , y ) + " " ) ;