Prime triplets consisting of values up to N having difference between two elements equal to the third | Stores 1 and 0 at indices which are prime and non - prime respectively ; Function to find all prime numbers from the range [ 0 , N ] ; Consider all numbers to prime initially ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all tultiples of p as false ; Function to find all prime triplets satisfying the given conditions ; Generate all primes up to N ; Stores the triplets ; Iterate over the range [ 3 , N ] ; Check for the condition ; Store the triplets ; Print all the stored triplets ; Driver code
var prime = new Array ( 100000 ) ; function SieveOfEratosthenes ( n ) { prime . fill ( true ) ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } function findTriplets ( N ) { SieveOfEratosthenes ( N ) ; var V = [ ] ; for ( var i = 3 ; i <= N ; i ++ ) { if ( 2 + i <= N && prime [ i ] == true && prime [ 2 + i ] == true ) { var a1 = [ 2 , i , i + 2 ] ; V . push ( a1 ) ; } } for ( var i = 0 ; i < V . length ; i ++ ) { document . write ( V [ i ] [ 0 ] + " " + V [ i ] [ 1 ] + " " + V [ i ] [ 2 ] + " " ) ; } } N = 8 ; findTriplets ( N ) ;