Print all distinct Coprime sets possible from 1 to N

Given an integer N, the task is to find all distinct co-prime sets up to the given integer N such that an element doesn’t appear in more than a set.

A number a is said to be co-prime with b if GCD(a, b) = 1.

Examples:

Input: N = 5
Output: (1, 2) (3, 4, 5)

Input: N = 6
Output: (1, 2) (3, 4) (5, 6)



Approach:

  • To solve the problem mentioned above, we can observe that if N is less than 4 then all the elements are already co-prime till N because they will always have GCD as 1. Thus, for N = [1, 3], the possible coprime sets are (1), (1, 2) and (1, 2, 3) respectively.
  • For all the values of N > 3, there are two possible cases:
    • If the value of N is even, then every set will contain 2 adjacent elements up to N itself since adjacent numbers are always co-prime to each other.
    • If the value for integer N is odd, then every set will contain 2 adjacent element except the last set which will have last three elements.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to print
// all distinct co-prime sets
// possible for numbers from 1 to N
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print all coprime sets
void coPrimeSet(int n)
{
  
    int firstadj, secadj;
  
    // Check if n is less than 4
    // then simply print all values till n
    if (n < 4) {
        cout << "( ";
        for (int i = 1; i <= n; i++)
            cout << i << ", ";
  
        cout << ")\n";
    }
  
    // For all the values of n > 3
    else {
  
        // Check if n is even
        // then every set will contain
        // 2 adjacent elements up-to n
        if (n % 2 == 0) {
            for (int i = 0; i < n / 2; i++) {
                firstadj = 2 * i + 1;
                secadj = 2 * i + 2;
  
                cout << "(" << firstadj
                     << ", " << secadj << ")\n";
            }
        }
        else {
  
            // if n is odd then every set will
            // contain 2 adjacent element
            // except the last set which
            // will have last three elements
            for (int i = 0; i < n / 2 - 1; i++)
  
            {
                firstadj = 2 * i + 1;
                secadj = 2 * i + 2;
  
                cout << "(" << firstadj
                     << ", " << secadj << ")\n";
            }
  
            // Last element for odd case
            cout << "(" << n - 2 << ", " << n - 1
                 << ", " << n << ")\n";
        }
    }
}
  
// Driver Code
int main()
{
    int n = 5;
  
    coPrimeSet(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to print
// all distinct co-prime sets
// possible for numbers from 1 to N
import java.util.*;
  
class GFG{
  
// Function to print all co-prime sets
static void coPrimeSet(int n)
{
    int firstadj, secadj;
  
    // Check if n is less than 4 then 
    // simply print all values till n
    if (n < 4)
    {
        System.out.print("( ");
        for(int i = 1; i <= n; i++)
           System.out.print(i + ", ");
  
        System.out.print(")\n");
    }
  
    // For all the values of n > 3
    else
    {
          
        // Check if n is even then 
        // every set will contain
        // 2 adjacent elements up-to n
        if (n % 2 == 0)
        {
            for(int i = 0; i < n / 2; i++)
            {
               firstadj = 2 * i + 1;
                 secadj = 2 * i + 2;
                 
               System.out.print("(" + firstadj + 
                               ", " + secadj + ")\n");
            }
        }
        else 
        {
  
            // If n is odd then every set will
            // contain 2 adjacent element
            // except the last set which
            // will have last three elements
            for(int i = 0; i < n / 2 - 1; i++)
            {
               firstadj = 2 * i + 1;
                 secadj = 2 * i + 2;
                 
               System.out.print("(" + firstadj + 
                               ", " + secadj + ")\n");
            }
              
            // Last element for odd case
            System.out.print("(" + (n - 2) + 
                            ", " +  ( n - 1) + 
                            ", " + n + ")\n");
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
    int n = 5;
  
    coPrimeSet(n);
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to print
# all distinct co-prime sets
# possible for numbers from 1 to N
  
# Function to prall co-prime sets
def coPrimeSet(n):
      
    firstadj = 0;
    secadj = 0;
  
    # Check if n is less than 4 then
    # simply prall values till n
    if (n < 4):
        print("( ");
          
        for i in range(1, n + 1):
            print(i + ", ");
        print(")");
  
    # For all the values of n > 3
    else:
  
        # Check if n is even then
        # every set will contain
        # 2 adjacent elements up-to n
        if (n % 2 == 0):
              
            for i in range(0, n /2 ):
                firstadj = 2 * i + 1;
                secadj = 2 * i + 2;
                    
                print("(", firstadj, ", ",
                           secadj, ")");
        else:
  
            # If n is odd then every set will
            # contain 2 adjacent element
            # except the last set which
            # will have last three elements
            for i in range(0, int(n / 2) - 1):
                firstadj = 2 * i + 1;
                secadj = 2 * i + 2;
                    
                print("(", firstadj, ", "
                           secadj, ")");
  
            # Last element for odd case
            print("(", (n - 2), ", "
                       (n - 1), ", ", n, ")");
                         
# Driver code
if __name__ == '__main__':
      
    n = 5;
  
    coPrimeSet(n);
      
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to print
// all distinct co-prime sets
// possible for numbers from 1 to N
using System;
  
class GFG{
  
// Function to print all co-prime sets
static void coPrimeSet(int n)
{
    int firstadj, secadj;
  
    // Check if n is less than 4 then 
    // simply print all values till n
    if (n < 4)
    {
        Console.Write("( ");
        for(int i = 1; i <= n; i++)
           Console.Write(i + ", ");
  
        Console.Write(")\n");
    }
  
    // For all the values of n > 3
    else
    {
          
        // Check if n is even then 
        // every set will contain
        // 2 adjacent elements up-to n
        if (n % 2 == 0)
        {
            for(int i = 0; i < n / 2; i++)
            {
               firstadj = 2 * i + 1;
                 secadj = 2 * i + 2;
                 
               Console.Write("(" + firstadj + 
                            ", " + secadj + ")\n");
            }
        }
        else
        {
  
            // If n is odd then every set will
            // contain 2 adjacent element
            // except the last set which
            // will have last three elements
            for(int i = 0; i < n / 2 - 1; i++)
            {
               firstadj = 2 * i + 1;
                 secadj = 2 * i + 2;
                  
               Console.Write("(" + firstadj + 
                            ", " + secadj + ")\n");
            }
              
            // Last element for odd case
            Console.Write("(" + (n - 2) + 
                         ", " + (n - 1) + 
                           ", " + n + ")\n");
        }
    }
}
  
// Driver code
public static void Main()
{
    int n = 5;
  
    coPrimeSet(n);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

(1, 2)
(3, 4, 5)

competitive-programming-img




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.