Open In App

Number of Quadruples with GCD equal to K

Given two integers N and K, the task is to find the count of quadruples of (i, j, k, l) such that 1 ? i < j < k < l ? N and gcd(i, j, k, l) = K.
Examples: 
 

Input: N = 10, K = 2 
Output:
Valid quadruples are (2, 4, 6, 8), (2, 4, 6, 10), 
(2, 4, 8, 10), (2, 6, 8, 10) and (4, 6, 8, 10)
Input: N = 8, K = 1 
Output: 69 
 



 

Approach: 
 



Below is the implementation of the above approach: 
 




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate NC4
int nCr(int n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
 
    int answer = n * (n - 1) * (n - 2) * (n - 3);
    answer /= 24;
    return answer;
}
 
// Function to return the count of required
// quadruples using Inclusion Exclusion
int countQuadruples(int N, int K)
{
    // Effective N
    int M = N / K;
 
    int answer = nCr(M);
 
    // Iterate over 2 to M
    for (int i = 2; i < M; i++) {
        int j = i;
 
        // Number of divisors of i till M
        int temp2 = M / i;
 
        // Count stores the number of prime
        // divisors occurring exactly once
        int count = 0;
 
        // To prevent repetition of prime divisors
        int check = 0;
        int temp = j;
 
        while (j % 2 == 0) {
            count++;
            j /= 2;
            if (count >= 2)
                break;
        }
 
        if (count >= 2) {
            check = 1;
        }
 
        for (int k = 3; k <= sqrt(temp); k += 2) {
            int cnt = 0;
 
            while (j % k == 0) {
                cnt++;
                j /= k;
                if (cnt >= 2)
                    break;
            }
 
            if (cnt >= 2) {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
 
        if (j > 2) {
            count++;
        }
 
        // If repetition of prime divisors present
        // ignore this number
        if (check)
            continue;
        else {
 
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1) {
                answer -= nCr(temp2);
            }
            else {
                answer += nCr(temp2);
            }
        }
    }
 
    return answer;
}
 
// Driver code
int main()
{
    int N = 10, K = 2;
 
    cout << countQuadruples(N, K);
 
    return 0;
}




// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to calculate NC4
static int nCr(int n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
 
    int answer = n * (n - 1) * (n - 2) * (n - 3);
    answer /= 24;
    return answer;
}
 
// Function to return the count of required
// quadruples using Inclusion Exclusion
static int countQuadruples(int N, int K)
{
    // Effective N
    int M = N / K;
 
    int answer = nCr(M);
 
    // Iterate over 2 to M
    for (int i = 2; i < M; i++)
    {
        int j = i;
 
        // Number of divisors of i till M
        int temp2 = M / i;
 
        // Count stores the number of prime
        // divisors occurring exactly once
        int count = 0;
 
        // To prevent repetition of prime divisors
        int check = 0;
        int temp = j;
 
        while (j % 2 == 0)
        {
            count++;
            j /= 2;
            if (count >= 2)
                break;
        }
 
        if (count >= 2)
        {
            check = 1;
        }
 
        for (int k = 3; k <= Math.sqrt(temp); k += 2)
        {
            int cnt = 0;
 
            while (j % k == 0)
            {
                cnt++;
                j /= k;
                if (cnt >= 2)
                    break;
            }
 
            if (cnt >= 2)
            {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
 
        if (j > 2)
        {
            count++;
        }
 
        // If repetition of prime divisors present
        // ignore this number
        if (check==1)
            continue;
        else
        {
 
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1)
            {
                answer -= nCr(temp2);
            }
            else
            {
                answer += nCr(temp2);
            }
        }
    }
 
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
 
    int N = 10, K = 2;
 
    System.out.println(countQuadruples(N, K));
}
}
 
// This code is contributed by Princi Singh




# Python3 implementation of the approach
from math import sqrt
 
# Function to calculate NC4
def nCr(n) :
 
    # Base case to calculate NC4
    if (n < 4) :
        return 0;
 
    answer = n * (n - 1) * (n - 2) * (n - 3);
    answer //= 24;
     
    return answer;
 
# Function to return the count of required
# quadruples using Inclusion Exclusion
def countQuadruples(N, K) :
 
    # Effective N
    M = N // K;
 
    answer = nCr(M);
 
    # Iterate over 2 to M
    for i in range(2, M) :
        j = i;
 
        # Number of divisors of i till M
        temp2 = M // i;
 
        # Count stores the number of prime
        # divisors occurring exactly once
        count = 0;
 
        # To prevent repetition of prime divisors
        check = 0;
        temp = j;
 
        while (j % 2 == 0) :
            count += 1;
            j //= 2;
            if (count >= 2) :
                break;
 
        if (count >= 2) :
            check = 1;
 
        for k in range(3, int(sqrt(temp)), 2) :
            cnt = 0;
 
            while (j % k == 0) :
                cnt += 1;
                j //= k;
                if (cnt >= 2) :
                    break;
     
            if (cnt >= 2) :
                check = 1;
                break;
            elif (cnt == 1) :
                count += 1;
 
        if (j > 2) :
            count += 1;
 
        # If repetition of prime divisors present
        # ignore this number
        if (check) :
            continue;
        else :
             
            # If prime divisor count is odd
            # subtract it from answer else add
            if (count % 2 == 1) :
                answer -= nCr(temp2);
            else :
                answer += nCr(temp2);
 
    return answer;
 
# Driver code
if __name__ == "__main__" :
 
    N = 10; K = 2;
 
    print(countQuadruples(N, K));
 
# This code is contributed by AnkitRai01




// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to calculate NC4
static int nCr(int n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
 
    int answer = n * (n - 1) * (n - 2) * (n - 3);
    answer /= 24;
    return answer;
}
 
// Function to return the count of required
// quadruples using Inclusion Exclusion
static int countQuadruples(int N, int K)
{
    // Effective N
    int M = N / K;
 
    int answer = nCr(M);
 
    // Iterate over 2 to M
    for (int i = 2; i < M; i++)
    {
        int j = i;
 
        // Number of divisors of i till M
        int temp2 = M / i;
 
        // Count stores the number of prime
        // divisors occurring exactly once
        int count = 0;
 
        // To prevent repetition of prime divisors
        int check = 0;
        int temp = j;
 
        while (j % 2 == 0)
        {
            count++;
            j /= 2;
            if (count >= 2)
                break;
        }
 
        if (count >= 2)
        {
            check = 1;
        }
 
        for (int k = 3; k <= Math.Sqrt(temp); k += 2)
        {
            int cnt = 0;
 
            while (j % k == 0)
            {
                cnt++;
                j /= k;
                if (cnt >= 2)
                    break;
            }
 
            if (cnt >= 2)
            {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
 
        if (j > 2)
        {
            count++;
        }
 
        // If repetition of prime divisors present
        // ignore this number
        if (check==1)
            continue;
        else
        {
 
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1)
            {
                answer -= nCr(temp2);
            }
            else
            {
                answer += nCr(temp2);
            }
        }
    }
 
    return answer;
}
 
// Driver code
public static void Main(String[] args)
{
 
    int N = 10, K = 2;
 
    Console.WriteLine(countQuadruples(N, K));
}
}
 
// This code is contributed by 29AjayKumar




<script>
 
// Javascript implementation of the approach
 
// Function to calculate NC4
function nCr(n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
 
    let answer = n * (n - 1) * (n - 2) * (n - 3);
    answer = parseInt(answer / 24);
    return answer;
}
 
// Function to return the count of required
// quadruples using Inclusion Exclusion
function countQuadruples(N, K)
{
    // Effective N
    let M = parseInt(N / K);
 
    let answer = nCr(M);
 
    // Iterate over 2 to M
    for (let i = 2; i < M; i++) {
        let j = i;
 
        // Number of divisors of i till M
        let temp2 = parseInt(M / i);
 
        // Count stores the number of prime
        // divisors occurring exactly once
        let count = 0;
 
        // To prevent repetition of prime divisors
        let check = 0;
        let temp = j;
 
        while (j % 2 == 0) {
            count++;
            j = parseInt(j / 2);
            if (count >= 2)
                break;
        }
 
        if (count >= 2) {
            check = 1;
        }
 
        for (let k = 3; k <= Math.sqrt(temp); k += 2)
        {
            let cnt = 0;
 
            while (j % k == 0) {
                cnt++;
                j = parseInt(j / k);
                if (cnt >= 2)
                    break;
            }
 
            if (cnt >= 2) {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
 
        if (j > 2) {
            count++;
        }
 
        // If repetition of prime divisors present
        // ignore this number
        if (check)
            continue;
        else {
 
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1) {
                answer -= nCr(temp2);
            }
            else {
                answer += nCr(temp2);
            }
        }
    }
 
    return answer;
}
 
// Driver code
    let N = 10, K = 2;
 
    document.write(countQuadruples(N, K));
 
</script>

Output: 
5

 

Time Complexity: O(sqrt(n)*n)

Auxiliary Space: O(1)


Article Tags :