Consecutive Prime numbers greater than equal to given number . | C ++ program for the above approach ; Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver Program
#include <bits/stdc++.h> NEW_LINE #define endl  " NEW_LINE " #define ll  long long NEW_LINE 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 ; }