Given two integers X and Y, the task is to find the number of integers, K, such that gcd(X, Y) is equal to gcd(X+K, Y), where 0 < K <Y.
Examples:
Input: X = 3, Y = 15
Output: 4
Explanation: All possible values of K are {0, 3, 6, 9} for which GCD(X, Y) = GCD(X + K, Y).
Input: X = 2, Y = 12
Output: 2
Explanation: All possible values of K are {0, 8}.
Naive Approach: The simplest approach to solve the problem is to iterate over the range [0, Y – 1]and for each value of i, check if GCD(X + i, Y) is equal to GCD(X, Y) or not.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int calculateK( int x, int y)
{
int count = 0;
int gcd_xy = gcd(x, y);
for ( int i = 0; i < y; i++) {
if (gcd(x + i, y) == gcd_xy)
count++;
}
return count;
}
int main()
{
int x = 3, y = 15;
cout << calculateK(x, y) << endl;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int calculateK( int x, int y)
{
int count = 0 ;
int gcd_xy = gcd(x, y);
for ( int i = 0 ; i < y; i++)
{
if (gcd(x + i, y) == gcd_xy)
count++;
}
return count;
}
public static void main(String[] args)
{
int x = 3 , y = 15 ;
System.out.print(calculateK(x, y));
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def calculateK(x, y):
count = 0
gcd_xy = gcd(x, y)
for i in range (y):
if (gcd(x + i, y) = = gcd_xy):
count + = 1
return count
if __name__ = = '__main__' :
x = 3
y = 15
print (calculateK(x, y))
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int calculateK( int x, int y)
{
int count = 0;
int gcd_xy = gcd(x, y);
for ( int i = 0; i < y; i++)
{
if (gcd(x + i, y) == gcd_xy)
count++;
}
return count;
}
public static void Main(String[] args)
{
int x = 3, y = 15;
Console.Write(calculateK(x, y));
}
}
|
Javascript
<script>
function gcd(a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
function calculateK(x , y) {
var count = 0;
var gcd_xy = gcd(x, y);
for (i = 0; i < y; i++) {
if (gcd(x + i, y) == gcd_xy)
count++;
}
return count;
}
var x = 3, y = 15;
document.write(calculateK(x, y));
</script>
|
Time Complexity: O(YlogY)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the concept of Euler’s totient function. Follow the steps below to solve the problem:
- Calculate the gcd of X and Y and store it in a variable g.
- Initialize a variable n with Y/g.
- Now, find the totient function for n which will be the required answer.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int calculateK( int x, int y)
{
int g = gcd(x, y);
int n = y / g;
int res = n;
for ( int 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;
}
int main()
{
int x = 3, y = 15;
cout << calculateK(x, y) << endl;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int calculateK( int x, int y)
{
int g = gcd(x, y);
int n = y / g;
int res = n;
for ( int 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;
}
public static void main(String[] args)
{
int x = 3 , y = 15 ;
System.out.print(calculateK(x, y) + "\n" );
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def calculateK(x, y):
g = gcd(x, y)
n = y / / g
res = n
i = 2
while i * i < = n:
if (n % i = = 0 ):
res - = (res / / i)
while (n % i = = 0 ):
n / / = i
i + = 1
if (n ! = 1 ):
res - = (res / / n)
return res
if __name__ = = "__main__" :
x = 3
y = 15
print (calculateK(x, y))
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int calculateK( int x, int y)
{
int g = gcd(x, y);
int n = y / g;
int res = n;
for ( int 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;
}
public static void Main(String[] args)
{
int x = 3, y = 15;
Console.Write(calculateK(x, y) + "\n" );
}
}
|
Javascript
<script>
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) + "\n" );
</script>
|
Time Complexity: O(log(min(X, Y)) + ?N) where N is Y/gcd(X, Y).
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Apr, 2021
Like Article
Save Article