Chen Prime Number | Utility function to check whether number is semiprime or not ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Utility function to check whether the given number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check Chen prime number ; Driver code
function isSemiprime ( num ) { var cnt = 0 ; for ( var i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) num /= i ++ cnt ; if ( num > 1 ) ++ cnt ; return cnt == 2 ; } function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function isChenPrime ( n ) { if ( isPrime ( n ) && ( isSemiprime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } var n = 7 ; if ( isChenPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;