Count prime triplets upto N having sum of first two elements equal to the third number | Javascript program for the above approach ; Boolean array to mark prime numbers ; To count the prime triplets having the sum of the first two numbers equal to the third element ; Function to count prime triplets having sum of the first two elements equal to the third element ; Sieve of Eratosthenes ; Checks for the prime numbers having difference equal to2 ; Update count of triplets ; Print the answer ; Driver Code
let MAX = 1000001 let prime = new Array ( MAX ) . fill ( true ) ; let cntTriplet = new Array ( MAX ) . fill ( 0 ) ; function primeTriplet ( N ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( let i = 3 ; i <= N ; i ++ ) { for ( let i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( let i = 3 ; i <= N ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) { cntTriplet [ i ] = cntTriplet [ i - 1 ] + 1 ; } else { cntTriplet [ i ] = cntTriplet [ i - 1 ] ; } } document . write ( cntTriplet [ N ] ) ; } let N = 7 ; primeTriplet ( N ) ;