Prime triplets consisting of values up to N having difference between two elements equal to the third | C # program for the above approach ; Stores 1 and 0 at indices which are prime and non - prime respectively ; Function to find all prime numbers from the range [ 0 , N ] ; 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
using System ; using System . Collections . Generic ; class GFG { static bool [ ] prime = new bool [ 100000 ] ; static void initialize ( ) { for ( int i = 0 ; i < 100000 ; i ++ ) prime [ i ] = true ; } static void SieveOfEratosthenes ( int n ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void findTriplets ( int N ) { SieveOfEratosthenes ( N ) ; List < List < int > > V = new List < List < int > > ( ) ; for ( int i = 3 ; i <= N ; i ++ ) { if ( 2 + i <= N && prime [ i ] == true && prime [ 2 + i ] ) { List < int > a1 = new List < int > ( ) ; a1 . Add ( 2 ) ; a1 . Add ( i ) ; a1 . Add ( i + 2 ) ; V . Add ( a1 ) ; } } for ( int i = 0 ; i < V . Count ; i ++ ) { Console . WriteLine ( V [ i ] [ 0 ] + " ▁ " + V [ i ] [ 1 ] + " ▁ " + V [ i ] [ 2 ] ) ; } } public static void Main ( ) { initialize ( ) ; int N = 8 ; findTriplets ( N ) ; } }