Count of quadruplets from range [L, R] having GCD equal to K

Given an integer K and a range [L, R], the task is to count the quadruplet pairs from the given range having gcd equal to K.

Examples:

Input: L = 1, R = 5, K = 3
Output: 1
(3, 3, 3, 3) is the only valid quadruplet with gcd = 3

Input: L = 2, R = 24, K = 5
Output: 239

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: We can iterate over all the numbers with four loops and for every quadruplet pair check whether its gcd is equal to K.

Below is the implementation of the above approach:

C++

 // C++ implementation of the approach #include using namespace std;    // Function to return // the gcd of a and b int gcd(int a, int b) {     if (b == 0)         return a;     return gcd(b, a % b); }    // Function to return the count // of quadruplets having gcd = k int countQuadruplets(int l, int r, int k) {        // To store the required count     int count = 0;        // Check every quadruplet pair     // whether its gcd is k     for (int u = l; u <= r; u++) {         for (int v = l; v <= r; v++) {             for (int w = l; w <= r; w++) {                 for (int x = l; x <= r; x++) {                     if (gcd(gcd(u, v), gcd(w, x)) == k)                         count++;                 }             }         }     }        // Return the required count     return count; }    // Driver code int main() {     int l = 1, r = 10, k = 2;        cout << countQuadruplets(l, r, k);        return 0; }

Java

 // Java implementation of the approach import java.io.*;    class GFG {        // Function to return     // the gcd of a and b     static int gcd(int a, int b)     {         if (b == 0)             return a;         return gcd(b, a % b);     }        // Function to return the count     // of quadruplets having gcd = k     static int countQuadruplets(int l, int r, int k)     {            // To store the required count         int count = 0;            // Check every quadruplet pair         // whether its gcd is k         for (int u = l; u <= r; u++) {             for (int v = l; v <= r; v++) {                 for (int w = l; w <= r; w++) {                     for (int x = l; x <= r; x++) {                         if (gcd(gcd(u, v), gcd(w, x)) == k)                             count++;                     }                 }             }         }            // Return the required count         return count;     }        // Driver code     public static void main(String[] args)     {            int l = 1, r = 10, k = 2;            System.out.println(countQuadruplets(l, r, k));     } }    // This code is contributed by jit_t.

C#

 // C# implementation of the approach using System;    class GFG {        // Function to return     // the gcd of a and b     static int gcd(int a, int b)     {         if (b == 0)             return a;         return gcd(b, a % b);     }        // Function to return the count     // of quadruplets having gcd = k     static int countQuadruplets(int l, int r, int k)     {            // To store the required count         int count = 0;            // Check every quadruplet pair         // whether its gcd is k         for (int u = l; u <= r; u++) {             for (int v = l; v <= r; v++) {                 for (int w = l; w <= r; w++) {                     for (int x = l; x <= r; x++) {                         if (gcd(gcd(u, v), gcd(w, x)) == k)                             count++;                     }                 }             }         }            // Return the required count         return count;     }        // Driver code     static public void Main()     {         int l = 1, r = 10, k = 2;         Console.WriteLine(countQuadruplets(l, r, k));     } }    // This code is contributed by ajit.

Output:

607

Time Complexity: O((r – l)4)

Efficient approach:

1. Find the GCD of every possible pair (x, y) in the given range.
2. Count the frequencies of every possible GCD value.
3. After that if the GCD value of two numbers is k then increment count by frequency[i] * frequency[j].

Below is the implementation of the above approach:

C++

 // C++ implementation of the approach #include using namespace std;    // Function to return // the gcd of a and b int gcd(int a, int b) {     if (b == 0)         return a;     return gcd(b, a % b); }    // Function to return the count // of quadruplets having gcd = k int countQuadruplets(int l, int r, int k) {        int frequency[r + 1] = { 0 };        // Count the frequency of every possible gcd     // value in the range     for (int i = l; i <= r; i++) {         for (int j = l; j <= r; j++) {             frequency[gcd(i, j)]++;         }     }        // To store the required count     long long answer = 0;        // Calculate the answer using frequency values     for (int i = 1; i <= r; i++) {         for (int j = 1; j <= r; j++) {             if (gcd(i, j) == k) {                 answer += (frequency[i] * frequency[j]);             }         }     }        // Return the required count     return answer; }    // Driver code int main() {     int l = 1, r = 10, k = 2;        cout << countQuadruplets(l, r, k);        return 0; }

Java

 // Java implementation of the approach  class GFG {        // Function to return  // the gcd of a and b  static int gcd(int a, int b)  {      if (b == 0)          return a;      return gcd(b, a % b);  }     // Function to return the count  // of quadruplets having gcd = k  static int countQuadruplets(int l, int r, int k)  {         int frequency[]= new int[r + 1] ;         // Count the frequency of every possible gcd      // value in the range      for (int i = l; i <= r; i++)     {          for (int j = l; j <= r; j++)          {              frequency[gcd(i, j)]++;          }      }         // To store the required count      long answer = 0;         // Calculate the answer using frequency values      for (int i = 1; i <= r; i++)     {          for (int j = 1; j <= r; j++)          {              if (gcd(i, j) == k)              {                  answer += (frequency[i] * frequency[j]);              }          }      }         // Return the required count      return (int)answer;  }     // Driver code  public static void main(String args[]) {      int l = 1, r = 10, k = 2;         System.out.println(countQuadruplets(l, r, k));  } }     // This code is contributed by Arnab Kundu

C#

 // C# implementation of the approach  using System;    class GFG {            // Function to return  // the gcd of a and b  static int gcd(int a, int b)  {      if (b == 0)          return a;      return gcd(b, a % b);  }     // Function to return the count  // of quadruplets having gcd = k  static int countQuadruplets(int l, int r, int k)  {         int []frequency= new int[r + 1] ;         // Count the frequency of every possible gcd      // value in the range      for (int i = l; i <= r; i++)     {          for (int j = l; j <= r; j++)          {              frequency[gcd(i, j)]++;          }      }         // To store the required count      long answer = 0;         // Calculate the answer using frequency values      for (int i = 1; i <= r; i++)     {          for (int j = 1; j <= r; j++)          {              if (gcd(i, j) == k)              {                  answer += (frequency[i] * frequency[j]);              }          }      }         // Return the required count      return (int)answer;  }     // Driver code  static public void Main () {     int l = 1, r = 10, k = 2;      Console.WriteLine(countQuadruplets(l, r, k));  } }     // This code is contributed by @ajit_00023

Output:

607

Time Complexity: O((r – l)2)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t, andrew1234, p_unit