Consecutive Prime numbers greater than equal to given number.

### 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:14Output:3 5Explanation: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++

`//C++ program for the above approach` `#include <bits/stdc++.h>` `#define endl "\n"` `#define ll long long` `using` `namespace` `std;` `//Function to check prime.` `bool` `is_prime(ll n)` `{` ` ` `if` `(n == 1)` ` ` `{` ` ` `return` `false` `;` ` ` `}` ` ` `for` `(ll i = 2; i <= ` `sqrt` `(n); i++)` ` ` `{` ` ` `if` `(n % i == 0)` ` ` `{` ` ` `// It means it is not ` ` ` `// a prime` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `// No factor other than 1 ` ` ` `// therefore prime number` ` ` `return` `true` `;` `}` `//Function to find out the required ` `//consecutive primes.` `void` `consecutive_primes(` `int` `n)` `{` ` ` `ll first = -1, second = -1;` ` ` ` ` `//Finding first prime just` ` ` `// less than sqrt(n).` ` ` `for` `(ll i = ` `sqrt` `(n); i >= 2; i--)` ` ` `{` ` ` `if` `(is_prime(i))` ` ` `{` ` ` `first = i;` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Finding prime just greater ` ` ` `//than sqrt(n).` ` ` `for` `(ll i = ` `sqrt` `(n) + 1; ` ` ` `i <= n / 2; i++)` ` ` `{` ` ` `if` `(is_prime(i))` ` ` `{` ` ` `second = i;` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Product of both prime is greater ` ` ` `// than n then print it` ` ` `if` `(first * second >= n)` ` ` `{` ` ` `cout << first << ` `" "` ` ` `<<second << endl;` ` ` `}` ` ` `// Finding prime greater than second` ` ` `else` ` ` `{` ` ` `for` `(ll i = second + 1; ` ` ` `i <= n; i++)` ` ` `{` ` ` `if` `(is_prime(i))` ` ` `{` ` ` `cout << second << ` `" "` ` ` `<< i << endl;` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `}` `}` `//Driver Program ` `int` `main()` `{` ` ` `ll n = 14;` ` ` `consecutive_primes(n);` ` ` `return` `0;` `}` |

**Output**

3 5

**Time Complexity: O(sqrt(n))**