Consecutive Prime numbers greater than equal to given number.
Last Updated :
27 May, 2021
Question:
Given a number n, the task is to find two consecutive prime such that the product of these two prime is greater than or equal to n.
Example:
Input: 14
Output: 3 5
Explanation: 3 and 5 are consecutive prime numbers whose product is greater than 14.
Approach:
Suppose n is of the range 10^8 to 10^10. We cannot find out primes using sieve because the range is upto10^10.
We can find the required consecutive primes by doing the following method.
- Find the greatest prime which is less than sqrt(n) and store it in a temporary variable (first).
- Find the smallest prime which is greater than sqrt(n) and store it in a temporary variable(second).
- If the product of first and second is greater than equal to n then print it.
- Else find a prime just greater than second and print it along with second.
Code:
C++
#include <bits/stdc++.h>
#define endl "\n"
#define ll long long
using namespace std;
bool is_prime(ll n)
{
if (n == 1)
{
return false ;
}
for (ll i = 2; i <= sqrt (n); i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
void consecutive_primes( int n)
{
ll first = -1, second = -1;
for (ll i = sqrt (n); i >= 2; i--)
{
if (is_prime(i))
{
first = i;
break ;
}
}
for (ll i = sqrt (n) + 1;
i <= n / 2; i++)
{
if (is_prime(i))
{
second = i;
break ;
}
}
if (first * second >= n)
{
cout << first << " "
<<second << endl;
}
else
{
for (ll i = second + 1;
i <= n; i++)
{
if (is_prime(i))
{
cout << second << " "
<< i << endl;
return ;
}
}
}
}
int main()
{
ll n = 14;
consecutive_primes(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean is_prime( long n)
{
if (n == 1 )
{
return false ;
}
for ( long i = 2 ; i <= ( long )Math.sqrt(n); i++)
{
if (n % i == 0 )
{
return false ;
}
}
return true ;
}
static void consecutive_primes( long n)
{
long first = - 1 , second = - 1 ;
for ( long i = ( long )Math.sqrt(n); i >= 2 ; i--)
{
if (is_prime(i))
{
first = i;
break ;
}
}
for ( long i = ( long )Math.sqrt(n) + 1 ;
i <= n / 2 ; i++)
{
if (is_prime(i))
{
second = i;
break ;
}
}
if (first * second >= n)
{
System.out.println(first + " " + second);
}
else
{
for ( long i = second + 1 ; i <= n; i++)
{
if (is_prime(i))
{
System.out.println(second + " " + i);
return ;
}
}
}
}
public static void main(String args[])
{
long n = 14 ;
consecutive_primes(n);
}
}
|
Python3
from math import sqrt
def is_prime(n):
if (n = = 1 ):
return False
for i in range ( 2 , int (sqrt(n)) + 1 , 1 ):
if (n % i = = 0 ):
return False
return True
def consecutive_primes(n):
first = - 1
second = - 1
i = int (sqrt(n))
while (i > = 2 ):
if (is_prime(i)):
first = i
break
i - = 1
for i in range ( int (sqrt(n)) + 1 ,n / / 2 + 1 , 1 ):
if (is_prime(i)):
second = i
break
if (first * second > = n):
print (first,second)
else :
for i in range (second + 1 ,n + 1 , 1 ):
if (is_prime(i)):
print (second,i)
return
if __name__ = = '__main__' :
n = 14
consecutive_primes(n)
|
C#
using System;
class GFG
{
static bool is_prime( long n)
{
if (n == 1) {
return false ;
}
for ( long i = 2; i <= ( long )Math.Sqrt(n); i++) {
if (n % i == 0)
{
return false ;
}
}
return true ;
}
static void consecutive_primes( long n)
{
long first = -1, second = -1;
for ( long i = ( long )Math.Sqrt(n); i >= 2; i--) {
if (is_prime(i)) {
first = i;
break ;
}
}
for ( long i = ( long )Math.Sqrt(n) + 1; i <= n / 2;
i++) {
if (is_prime(i)) {
second = i;
break ;
}
}
if (first * second >= n) {
Console.WriteLine(first + " " + second);
}
else {
for ( long i = second + 1; i <= n; i++) {
if (is_prime(i)) {
Console.WriteLine(second + " " + i);
return ;
}
}
}
}
public static void Main()
{
long n = 14;
consecutive_primes(n);
}
}
|
Javascript
<script>
function is_prime(n)
{
if (n == 1)
{
return false ;
}
for ( var i = 2; i <= parseInt(Math.sqrt(n)); i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
function consecutive_primes(n)
{
var first = -1, second = -1;
for ( var i = parseInt(Math.sqrt(n)); i >= 2; i--)
{
if (is_prime(i))
{
first = i;
break ;
}
}
for ( var i = parseInt(Math.sqrt(n)) + 1;
i <= parseInt(n / 2); i++)
{
if (is_prime(i))
{
second = i;
break ;
}
}
if (first * second >= n)
{
document.write(first + " " + second);
}
else
{
for ( var i = second + 1; i <= n; i++)
{
if (is_prime(i))
{
document.write(second + " " + i);
return ;
}
}
}
}
var n = 14;
consecutive_primes(n);
</script>
|
Time Complexity: O(sqrt(n))
Share your thoughts in the comments
Please Login to comment...