Check if an integer can be expressed as a sum of two semi | Javascript Code to check if an integer can be expressed as sum of two semi - primes ; Utility function to compute semi - primes in a range ; num /= j , ++ cnt ; 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 ; if count is equal to '2' then number is semi - prime ; Utility function to check if a number sum of two semi - primes ; arr [ i ] is already a semi - prime if n - arr [ i ] is also a semi - prime then we a number can be expressed as sum of two semi - primes ; Driver code
var MAX = 1000000 ; var arr = [ ] ; var sprime = Array ( MAX ) . fill ( false ) ; function computeSemiPrime ( ) { for ( var i = 2 ; i < MAX ; i ++ ) { var cnt = 0 ; var num = i ; for ( var j = 2 ; cnt < 2 && j * j <= num ; ++ j ) { while ( num % j == 0 ) { } } if ( num > 1 ) ++ cnt ; if ( cnt == 2 ) { sprime [ i ] = true ; arr . push ( i ) ; } } } function checkSemiPrime ( n ) { var i = 0 ; while ( arr [ i ] <= parseInt ( n / 2 ) ) { if ( sprime [ n - arr [ i ] ] ) { return true ; } i ++ ; } return false ; } computeSemiPrime ( ) ; var n = 30 ; if ( checkSemiPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;