📜  打印通过将质数加到N形成的最接近的质数

📅  最后修改于: 2021-04-23 06:27:21             🧑  作者: Mango

给定数字N。如果数字不是素数,则任务是打印最接近的素数,方法是从2开始依次添加素数以使其成为素数。

例子:

方法使用Eratosthenes筛,在isprime []列表中以1标记质数索引,并将所有质数存储在列表prime []中。继续按顺序将质数加到N,直到它变成质数为止。

下面是上述方法的实现:

C++
// C++ program to print the 
// nearest prime number by
// sequentially adding the
// prime numbers 
#include
using namespace std;
  
// Function to store prime
// numbers using prime sieve
void prime_sieve(int MAX, vector &isprime,
                          vector &prime)
{
      
    // iterate for all
    // the numbers 
    int i = 2;
    while (i * i <= MAX)
    {
          
        // If prime[p] is not changed, 
        // then it is a prime
        if (isprime[i] == 1)
        {
              
            // append the prime
            // to the list 
            prime.push_back(i);
              
            // Update all multiples of p
            for (int j = i * 2; j < MAX; j += i)
            {
                isprime[j] = 0;
            }
        }
                  
        i += 1;
    }
}
          
// Function to print 
// the nearest prime 
int printNearest(int N)
{
    int MAX = 1e6;
      
    // store all the 
    // index with 1 
    vector isprime(MAX, 1);
  
    // 0 and 1 are not prime 
    isprime[0] = isprime[1] = 0;
      
    // list to store 
    // prime numbers
    vector prime;
      
    // variable to
    // add primes 
    int i = 0;
      
    // call the sieve function 
    prime_sieve(MAX, isprime, prime);
      
    // Keep on adding prime 
    // numbers till the nearest 
    // prime number is achieved 
      
    while (!isprime[N])
    {
        N += prime[i];
        i += 1;
    }
      
    // return the 
    // nearest prime 
    return N ;
}
  
// Driver Code 
int main()
{
    int N = 8;
    printf("%d", printNearest(N));
    return 0;
}
  
// This code is contributed
// by Harshit Saini


Java
// Java program to print the 
// nearest prime number by
// sequentially adding the
// prime numbers 
import java.util.*;
  
class GFG 
{
  
// Function to store prime
// numbers using prime sieve
static void prime_sieve(int MAX, int []isprime,
                        Vector prime)
{
      
    // iterate for all
    // the numbers 
    int i = 2;
    while (i * i <= MAX)
    {
          
        // If prime[p] is not changed, 
        // then it is a prime
        if (isprime[i] == 1)
        {
              
            // append the prime
            // to the list 
            prime.add(i);
              
            // Update all multiples of p
            for (int j = i * 2;
                     j < MAX; j += i)
            {
                isprime[j] = 0;
            }
        }
                  
        i += 1;
    }
}
          
// Function to print 
// the nearest prime 
static int printNearest(int N)
{
    int MAX = (int) 1e6;
      
    // store all the 
    // index with 1 except 0,1 index 
    int [] isprime = new int[MAX];
    for(int i = 2; i < MAX; i++)
        isprime[i] = 1;
      
    // list to store 
    // prime numbers
    Vector prime = new Vector();
      
    // variable to add primes 
    int i = 0;
      
    // call the sieve function 
    prime_sieve(MAX, isprime, prime);
      
    // Keep on adding prime 
    // numbers till the nearest 
    // prime number is achieved 
    while (isprime[N] == 0)
    {
        N += prime.get(i);
        i += 1;
    }
      
    // return the 
    // nearest prime 
    return N ;
}
  
// Driver Code 
public static void main(String[] args)
{
    int N = 8;
    System.out.printf("%d", printNearest(N));
}
} 
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to print the nearest prime 
# number by sequentially adding the prime numbers 
  
# Function to store prime numbers using prime sieve
def prime_sieve(MAX, isprime, prime):
      
    # iterate for all the numbers 
    i = 2
    while (i * i <= MAX):
           
        # If prime[p] is not changed, 
        # then it is a prime
        if (isprime[i] == 1):
              
            # append the prime to the list 
            prime.append(i)
              
            # Update all multiples of p
            for j in range(i * 2, MAX, i):
                isprime[j] = 0
                  
        i += 1
          
          
  
# Function to print the nearest prime 
def printNearest(N):
      
    MAX = 10**6 
      
    # store all the index with 1 
    isprime = [1] * MAX
      
    # 0 and 1 are not prime 
    isprime[0] = isprime[1] = 0 
      
    # list to store prime numbers
    prime = []
      
    # variable to add primes 
    i = 0
      
    # call the sieve function 
    prime_sieve(MAX, isprime, prime)
      
    # Keep on adding prime numbers 
    # till the nearest prime number 
    # is achieved 
    while not isprime[N]:
        N += prime[i]
        i += 1
      
    # return the nearest prime 
    return N 
    
  
# Driver Code 
N = 8
print(printNearest(N))


C#
// C# program to print the 
// nearest prime number by
// sequentially adding the
// prime numbers
using System;
using System.Collections.Generic;
      
class GFG 
{
  
// Function to store prime
// numbers using prime sieve
static void prime_sieve(int MAX, int []isprime,
                        List prime)
{
      
    // iterate for all the numbers 
    int i = 2;
    while (i * i <= MAX)
    {
          
        // If prime[p] is not changed, 
        // then it is a prime
        if (isprime[i] == 1)
        {
              
            // append the prime to the list 
            prime.Add(i);
              
            // Update all multiples of p
            for (int j = i * 2;
                     j < MAX; j += i)
            {
                isprime[j] = 0;
            }
        }
                  
        i += 1;
    }
}
          
// Function to print 
// the nearest prime 
static int printNearest(int N)
{
    int MAX = (int) 1e6;
    int i = 0;
      
    // store all the 
    // index with 1 except 0,1 index 
    int [] isprime = new int[MAX];
    for(i = 2; i < MAX; i++)
        isprime[i] = 1;
      
    // list to store 
    // prime numbers
    List prime = new List();
      
    // variable to add primes 
    i = 0;
      
    // call the sieve function 
    prime_sieve(MAX, isprime, prime);
      
    // Keep on adding prime 
    // numbers till the nearest 
    // prime number is achieved 
    while (isprime[N] == 0)
    {
        N += prime[i];
        i += 1;
    }
      
    // return the 
    // nearest prime 
    return N;
}
  
// Driver Code 
public static void Main(String[] args)
{
    int N = 8;
    Console.Write("{0}", printNearest(N));
}
}
  
// This code is contributed by Princi Singh


输出:
13