Consecutive Prime numbers greater than equal to given number . | Java 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 code
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 ) ; } }