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