The Wayback Machine - https://web.archive.org/web/20210113000358/https://www.geeksforgeeks.org/count-all-possible-values-of-k-less-than-y-such-that-gcdx-y-gcdxk-y/
Related Articles

Related Articles

Count all possible values of K less than Y such that GCD(X, Y) = GCD(X+K, Y)
  • Last Updated : 13 Jan, 2021

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <iostream>
using namespace std;
  
// Function to calculate
// GCD of two integers
int gcd(int a, int b)
{
    if (b == 0)
        return a;
  
    return gcd(b, a % b);
}
  
// Function to count possible
// values of K
int calculateK(int x, int y)
{
    int count = 0;
    int gcd_xy = gcd(x, y);
    for (int i = 0; i < y; i++) {
  
        // If required condition
        // is satisfied
        if (gcd(x + i, y) == gcd_xy)
  
            // Increase count
            count++;
    }
  
    return count;
}
  
// Driver Code
int main()
{
  
    // Given X and y
    int x = 3, y = 15;
  
    cout << calculateK(x, y) << endl;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to calculate
# GCD of two integers
def gcd(a, b):
      
    if (b == 0):
        return a
  
    return gcd(b, a % b)
  
# Function to count possible
# values of K
def calculateK(x, y):
      
    count = 0
    gcd_xy = gcd(x, y)
  
    for i in range(y):
          
        # If required condition
        # is satisfied
        if (gcd(x + i, y) == gcd_xy):
              
            # Increase count
            count += 1
  
    return count
  
# Driver Code
if __name__ == '__main__':
      
    # Given X and y
    x = 3
    y = 15
  
    print (calculateK(x, y))
  
# This code is contributed by mohit kumar 29

chevron_right


Output: 

4

 

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <iostream>
using namespace std;
  
// Function to find the gcd of a and b
int gcd(int a, int b)
{
  
    if (b == 0)
        return a;
  
    return gcd(b, a % b);
}
  
// Function to find the number of Ks
int calculateK(int x, int y)
{
  
    // Find gcd
    int g = gcd(x, y);
    int n = y / g;
    int res = n;
  
    // Calculating value of totient
    // function for 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;
}
  
// Driver Code
int main()
{
  
    // Given X and Y
    int x = 3, y = 15;
  
    cout << calculateK(x, y) << endl;
}

chevron_right


Output: 

4

 

Time Complexity: O(log(min(X, Y)) + √N) where N is Y/gcd(X, Y).
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :