📜  程序查找下一个素数

📅  最后修改于: 2021-05-07 18:31:20             🧑  作者: Mango

给定整数N。任务是找到下一个质数,即大于N的最小质数。
例子:

方法:

  1. 首先,采用找到的布尔变量并将其初始化为false。
  2. 现在,直到该变量不等于true为止,在每次迭代中将N递增1并检查其是否为质数。
  3. 如果是素数,则打印它,并将找到的变量的值更改为True。否则,重复循环直到获得下一个素数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if n
// is prime else returns false
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;
   
    // This is checked so that we can skip 
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;
   
    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;
   
    return true;
}
 
// Function to return the smallest
// prime number greater than N
int nextPrime(int N)
{
 
    // Base case
    if (N <= 1)
        return 2;
 
    int prime = N;
    bool found = false;
 
    // Loop continuously until isPrime returns
    // true for a number greater than n
    while (!found) {
        prime++;
 
        if (isPrime(prime))
            found = true;
    }
 
    return prime;
}
 
// Driver code
int main()
{
    int N = 3;
 
    cout << nextPrime(N);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Function that returns true if n
    // is prime else returns false
    static boolean isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
         
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0) return false;
         
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
            return false;
         
        return true;
    }
     
    // Function to return the smallest
    // prime number greater than N
    static int nextPrime(int N)
    {
     
        // Base case
        if (N <= 1)
            return 2;
     
        int prime = N;
        boolean found = false;
     
        // Loop continuously until isPrime returns
        // true for a number greater than n
        while (!found)
        {
            prime++;
     
            if (isPrime(prime))
                found = true;
        }
     
        return prime;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 3;
     
        System.out.println(nextPrime(N));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
import math
 
# Function that returns True if n
# is prime else returns False
def isPrime(n):
     
    # Corner cases
    if(n <= 1):
        return False
    if(n <= 3):
        return True
     
    # This is checked so that we can skip
    # middle five numbers in below loop
    if(n % 2 == 0 or n % 3 == 0):
        return False
     
    for i in range(5,int(math.sqrt(n) + 1), 6):
        if(n % i == 0 or n % (i + 2) == 0):
            return False
     
    return True
 
# Function to return the smallest
# prime number greater than N
def nextPrime(N):
 
    # Base case
    if (N <= 1):
        return 2
 
    prime = N
    found = False
 
    # Loop continuously until isPrime returns
    # True for a number greater than n
    while(not found):
        prime = prime + 1
 
        if(isPrime(prime) == True):
            found = True
 
    return prime
 
# Driver code
N = 3
print(nextPrime(N))
 
# This code is contributed by Sanjit_Prasad


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
    // Function that returns true if n
    // is prime else returns false
    static bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
         
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
         
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 ||
                n % (i + 2) == 0)
            return false;
         
        return true;
    }
     
    // Function to return the smallest
    // prime number greater than N
    static int nextPrime(int N)
    {
     
        // Base case
        if (N <= 1)
            return 2;
     
        int prime = N;
        bool found = false;
     
        // Loop continuously until isPrime
        // returns true for a number
        // greater than n
        while (!found)
        {
            prime++;
     
            if (isPrime(prime))
                found = true;
        }
        return prime;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int N = 3;
     
        Console.WriteLine(nextPrime(N));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
5