📌  相关文章
📜  打印从1到N的所有不同的Coprime集

📅  最后修改于: 2021-05-07 08:52:25             🧑  作者: Mango

给定一个整数N ,任务是找到直至给定整数N的所有不同的互素数集,以使一个元素出现的次数不超过一个集合。

例子:

方法:

  • 为了解决上述问题,我们可以观察到,如果N小于4,则所有元素都已经互质素数直到N,因为它们的GCD始终为1。因此,对于N = [1,3],可能互素集分别是(1),(1、2)和(1、2、3)。
  • 对于N> 3的所有值,有两种可能的情况:
    • 如果N的值是偶数,则每个集合将包含2个相邻的元素,直到N个本身为止,因为相邻的数字始终互质为互素。
    • 如果整数N的值是奇数,则每个集合将包含2个相邻元素,但最后一个集合将具有最后三个元素。

下面是上述方法的实现:

C++
// C++ implementation to print
// all distinct co-prime sets
// possible for numbers from 1 to N
  
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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


输出:
(1, 2)
(3, 4, 5)