Program to find the next prime number
Given an integer N. The task is to find the next prime number i.e. the smallest prime number greater than N.
Examples:
Input: N = 10
Output: 11
11 is the smallest prime number greater than 10.
Input: N = 0
Output: 2
Approach:
- First of all, take a boolean variable found and initialize it to false.
- Now, until that variable not equals to true, increment N by 1 in each iteration and check whether it is prime or not.
- If it is prime then print it and change value of found variable to True. otherwise, iterate the loop until you will get the next prime number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1) return false ;
if (n <= 3) return true ;
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 ;
}
int nextPrime( int N)
{
if (N <= 1)
return 2;
int prime = N;
bool found = false ;
while (!found) {
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
int main()
{
int N = 3;
cout << nextPrime(N);
return 0;
}
|
Java
class GFG
{
static boolean isPrime( int n)
{
if (n <= 1 ) return false ;
if (n <= 3 ) return true ;
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 ;
}
static int nextPrime( int N)
{
if (N <= 1 )
return 2 ;
int prime = N;
boolean found = false ;
while (!found)
{
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
public static void main (String[] args)
{
int N = 3 ;
System.out.println(nextPrime(N));
}
}
|
Python3
import math
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
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
def nextPrime(N):
if (N < = 1 ):
return 2
prime = N
found = False
while ( not found):
prime = prime + 1
if (isPrime(prime) = = True ):
found = True
return prime
N = 3
print (nextPrime(N))
|
C#
using System;
class GFG
{
static bool isPrime( int n)
{
if (n <= 1) return false ;
if (n <= 3) return true ;
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 ;
}
static int nextPrime( int N)
{
if (N <= 1)
return 2;
int prime = N;
bool found = false ;
while (!found)
{
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
public static void Main (String[] args)
{
int N = 3;
Console.WriteLine(nextPrime(N));
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1) return false ;
if (n <= 3) return true ;
if (n%2 == 0 || n%3 == 0) return false ;
for (let i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false ;
return true ;
}
function nextPrime(N)
{
if (N <= 1)
return 2;
let prime = N;
let found = false ;
while (!found) {
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
let N = 3;
document.write(nextPrime(N));
</script>
|
Last Updated :
27 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...