原文:

给定一个数 n,任务是如果这个数不是素数,则打印最近的素数,方法是从 2 开始依次添加素数,使其成为素数。 例:

输入: n = 8 输出: 13 8 不是素数,所以加上第一个素数得到 10 10 不是素数,因此加上第二个素数,即 3 得到 13,即素数。 输入: n = 45 输出: 47

逼近使用厄拉多塞的筛,在is prime【】列表中用 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

python 3

# 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

java 描述语言


output: 

13

时间复杂度:o(n * log(logn)) t3】辅助空间: o(n)