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