📌  相关文章
📜  找出一个N个素数的序列,它们的和是一个合成数

📅  最后修改于: 2021-04-24 15:25:56             🧑  作者: Mango

给定一个整数N ,任务是找到一个N个素数的序列,它们的总和是一个合成数。

例子:

方法:两个质数的总和始终是偶数,因为它们是除2以外的奇数。现在有两种情况

  1. N为偶数时,我们可以打印除2外的任何N个素数,它们的总和将始终为偶数,即,将偶数相加后的奇数将得到偶数和。
  2. N为奇数时,我们可以打印2和任何其他N – 1个质数,以确保总和为偶数。因为N – 1是偶数,所以除2以外的所有素数的和都将是偶数,然后我们将2作为第N数字加起来,以确保总和保持偶数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAXN 100000
  
// To store prime numbers
vector v;
  
// Function to find and store
// all the primes <= n
void SieveOfEratosthenes(int n)
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p <= n; p++) {
  
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true) {
  
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Store all the prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            v.push_back(p);
}
  
// Function to print the required sequence
void GetSequence(int n)
{
  
    // If n is even then we do not include 2
    // and start the sequence from the 2nd
    // smallest prime else we include 2
    int i = (n % 2 == 0) ? 1 : 0;
  
    int cnt = 0;
    // Print the sequence
    while (cnt < n) {
        cout << v[i] << " ";
        i++;
        cnt++;
    }
}
  
// Driver code
int main()
{
    int n = 6;
    SieveOfEratosthenes(MAXN);
  
    GetSequence(n);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG
{
      
static int MAXN = 100000;
  
// To store prime numbers
static Vector v = new Vector();
  
// Function to find and store
// all the primes <= n
static void SieveOfEratosthenes(int n)
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    boolean[] prime = new boolean[n + 1];
    Arrays.fill(prime,true);
  
    for (int p = 2; p * p <= n; p++)
    {
  
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true) 
        {
  
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Store all the prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            v.add(p);
}
  
// Function to print the required sequence
static void GetSequence(int n)
{
  
    // If n is even then we do not include 2
    // and start the sequence from the 2nd
    // smallest prime else we include 2
    int i = (n % 2 == 0) ? 1 : 0;
  
    int cnt = 0;
      
    // Print the sequence
    while (cnt < n)
    {
        System.out.print(v.get(i) + " ");
        i++;
        cnt++;
    }
}
  
// Driver code
public static void main(String[] args)
{
    int n = 6;
    SieveOfEratosthenes(MAXN);
  
    GetSequence(n);
}
}
  
// This code is contributed by Princi Singh


python
# Python3 implementation of the approach
  
MAXN=100000
  
# To store prime numbers
v=[]
  
# Function to find and store
# all the primes <= n
def SieveOfEratosthenes(n):
  
    # Create a boolean array "prime[0..n]" and initialize
    # all entries it as true. A value in prime[i] will
    # finally be false if i is Not a prime, else true.
    prime=[True for i in range(n + 1)]
  
    for p in range(2,n+1):
  
        # If prime[p] is not changed, then it is a prime
        if (prime[p] == True):
  
            # Update all multiples of p greater than or
            # equal to the square of it
            # numbers which are multiple of p and are
            # less than p^2 are already been marked.
            for i in range(2 * p, n + 1, p):
                prime[i] = False
  
    # Store all the prime numbers
    for p in range(2, n + 1):
        if (prime[p]):
            v.append(p)
  
# Function to print the required sequence
def GetSequence(n):
  
    # If n is even then we do not include 2
    # and start the sequence from the 2nd
    # smallest prime else we include 2
    if n % 2 == 0:
        i = 1
    else:
        i = 0
  
    cnt = 0
    # Print the sequence
    while (cnt < n):
        print(v[i],end=" ")
        i += 1
        cnt += 1
  
  
# Driver code
n = 6
SieveOfEratosthenes(MAXN)
  
GetSequence(n)
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{ 
      
static int MAXN = 100000; 
  
// To store prime numbers 
static List v = new List(); 
  
// Function to find and store 
// all the primes <= n 
static void SieveOfEratosthenes(int n) 
{ 
    // Create a boolean array "prime[0..n]" and initialize 
    // all entries it as true. A value in prime[i] will 
    // finally be false if i is Not a prime, else true. 
    Boolean[] prime = new Boolean[n + 1]; 
    for(int i = 0; i < n + 1; i++)
        prime[i] = true;
  
    for (int p = 2; p * p <= n; p++) 
    { 
  
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
  
            // Update all multiples of p greater than or 
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked. 
            for (int i = p * p; i <= n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    // Store all the prime numbers 
    for (int p = 2; p <= n; p++) 
        if (prime[p]) 
            v.Add(p); 
} 
  
// Function to print the required sequence 
static void GetSequence(int n) 
{ 
  
    // If n is even then we do not include 2 
    // and start the sequence from the 2nd 
    // smallest prime else we include 2 
    int i = (n % 2 == 0) ? 1 : 0; 
  
    int cnt = 0; 
      
    // Print the sequence 
    while (cnt < n) 
    { 
        Console.Write(v[i] + " "); 
        i++; 
        cnt++; 
    } 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    int n = 6; 
    SieveOfEratosthenes(MAXN); 
  
    GetSequence(n); 
} 
} 
  
/* This code is contributed by PrinciRaj1992 */


输出:
3 5 7 11 13 17