Print all distinct Coprime sets possible from 1 to N | Function to print all co - prime 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
function coPrimeSet ( n ) { let firstadj , secadj ; if ( n < 4 ) { document . write ( " " ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( i + " " ) ; document . write ( " " + " " ) ; } else { if ( n % 2 == 0 ) { for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; document . write ( " " + firstadj + " " + secadj + " " + " " ) ; } } else { for ( let i = 0 ; i < Math . floor ( n / 2 ) - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; document . write ( " " + firstadj + " " + secadj + " " + " " ) ; } document . write ( " " + ( n - 2 ) + " " + ( n - 1 ) + " " + n + " " + " " ) ; } } } let n = 5 ; coPrimeSet ( n ) ;