Count all possible values of K less than Y such that GCD(X, Y) = GCD(X+K, Y)

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++
// C++ program for the above approachÂ
#include <iostream>using namespace std;Â
// Function to calculate// GCD of two integersint gcd(int a, int b){Â Â Â Â if (b == 0)Â Â Â Â Â Â Â Â return a;Â
    return gcd(b, a % b);}Â
// Function to count possible// values of Kint 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 Codeint main(){Â
    // Given X and y    int x = 3, y = 15;Â
    cout << calculateK(x, y) << endl;} |
Java
// Java program for the above approach import java.util.*;class GFG{       // Function to calculate // GCD of two integers static int gcd(int a, int b) {     if (b == 0)         return a;      return gcd(b, a % b); }    // Function to count possible // values of K static 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 codepublic static void main(String[] args){    // Given X and y     int x = 3, y = 15;     System.out.print(calculateK(x, y)); }}Â
// This code is contributed by sanjoy_62 |
Python3
# Python3 program for the above approachÂ
# Function to calculate# GCD of two integersdef gcd(a, b):Â Â Â Â Â Â Â Â Â if (b == 0):Â Â Â Â Â Â Â Â return aÂ
    return gcd(b, a % b)Â
# Function to count possible# values of Kdef 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 Codeif __name__ == '__main__':         # Given X and y    x = 3    y = 15Â
    print (calculateK(x, y))Â
# This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System;Â
class GFG{       // Function to calculate // GCD of two integers static int gcd(int a, int b) {     if (b == 0)         return a;              return gcd(b, a % b); }    // Function to count possible // values of K static 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 codepublic static void Main(String[] args){         // Given X and y     int x = 3, y = 15;          Console.Write(calculateK(x, y)); }}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// JavaScript program for the above approach Â
    // Function to calculate    // GCD of two integers    function gcd(a , b) {        if (b == 0)            return a;        return gcd(b, a % b);    }Â
    // Function to count possible    // values of K    function calculateK(x , y) {        var count = 0;        var gcd_xy = gcd(x, y);        for (i = 0; i < y; i++) {Â
            // If required condition            // is satisfied            if (gcd(x + i, y) == gcd_xy)Â
                // Increase count                count++;        }        return count;    }Â
    // Driver code             // Given X and y        var x = 3, y = 15;        document.write(calculateK(x, y));Â
// This code is contributed by todaysgaurav Â
</script> |
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++
// C++ program for the above approachÂ
#include <iostream>using namespace std;Â
// Function to find the gcd of a and bint gcd(int a, int b){Â
    if (b == 0)        return a;Â
    return gcd(b, a % b);}Â
// Function to find the number of Ksint 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 Codeint main(){Â
    // Given X and Y    int x = 3, y = 15;Â
    cout << calculateK(x, y) << endl;} |
Java
// Java program for the above approachimport java.util.*;class GFG{Â
// Function to find the gcd of a and bstatic int gcd(int a, int b){Â
    if (b == 0)        return a;    return gcd(b, a % b);}Â
// Function to find the number of Ksstatic 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 Codepublic static void main(String[] args){Â
    // Given X and Y    int x = 3, y = 15;    System.out.print(calculateK(x, y) +"\n");}}Â
// This code is contributed by shikhasingrajput |
Python3
# Python 3 program for the above approachÂ
# Function to find the gcd of a and bdef gcd(a, b):    if (b == 0):        return a    return gcd(b, a % b)Â
# Function to find the number of Ksdef calculateK(x, y):Â
    # Find gcd    g = gcd(x, y)    n = y // g    res = nÂ
    # Calculating value of totient    # function for 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Â
# Driver Codeif __name__ == "__main__":Â
    # Given X and Y    x = 3    y = 15Â
    print(calculateK(x, y))         # This code is contributed by chitranayal. |
C#
// C# program for the above approachusing System;Â
class GFG{Â
// Function to find the gcd of a and bstatic int gcd(int a, int b){Â Â Â Â if (b == 0)Â Â Â Â Â Â Â Â return a;Â Â Â Â Â Â Â Â Â Â Â Â Â return gcd(b, a % b);}Â
// Function to find the number of Ksstatic 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 Codepublic static void Main(String[] args){Â Â Â Â Â Â Â Â Â // Given X and YÂ Â Â Â int x = 3, y = 15;Â Â Â Â Â Â Â Â Â Console.Write(calculateK(x, y) + "\n");}}Â
// This code is contributed by gauravrajput1 |
Javascript
<script>// javascript program for the above approachÂ
    // Function to find the gcd of a and b    function gcd(a , b) {Â
        if (b == 0)            return a;        return gcd(b, a % b);    }Â
    // Function to find the number of Ks    function calculateK(x , y) {Â
        // Find gcd        var g = gcd(x, y);        var n = y / g;        var res = n;Â
        // Calculating value of totient        // function for 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;    }Â
    // Driver Code             // Given X and Y        var x = 3, y = 15;        document.write(calculateK(x, y) + "\n");Â
// This code is contributed by umadevi9616</script> |
4
Â
Time Complexity: O(log(min(X, Y)) + ?N) where N is Y/gcd(X, Y).
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



