Count distinct prime triplets up to N such that sum of two primes is equal to the third prime | Function to check if a number is a prime or not ; Function to count the number of valid prime triplets ; Stores the count of prime triplets ; Iterate from 2 to N and check for each p , whether p & ( p - 2 ) are prime or not ; Print the count obtained ; Driver Code
function isPrime ( N ) { if ( N <= 1 ) return false ; for ( let i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } function countPrimeTuples ( N ) { let count = 0 ; for ( let i = 2 ; i <= N ; i ++ ) { if ( isPrime ( i ) && isPrime ( i - 2 ) ) count ++ ; } document . write ( count ) ; } let N = 6 ; countPrimeTuples ( N ) ;