Prime Triplet | C # program to find prime triplets smaller than or equal to n . ; function to detect prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; function to print prime triplets ; Finding all primes from 1 to n ; triplets of form ( p , p + 2 , p + 6 ) ; triplets of form ( p , p + 4 , p + 6 ) ; Driver Code
using System ; class GFG { static void sieve ( int n , bool [ ] prime ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } } static void printPrimeTriplets ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; sieve ( n , prime ) ; Console . WriteLine ( " The ▁ prime ▁ triplets ▁ " + " from ▁ 1 ▁ to ▁ " + n + " ▁ are ▁ : " ) ; for ( int i = 2 ; i <= n - 6 ; ++ i ) { if ( ! prime [ i ] && ! prime [ i + 2 ] && ! prime [ i + 6 ] ) Console . WriteLine ( i + " ▁ " + ( i + 2 ) + " ▁ " + ( i + 6 ) ) ; else if ( ! prime [ i ] && ! prime [ i + 4 ] && ! prime [ i + 6 ] ) Console . WriteLine ( i + " ▁ " + ( i + 4 ) + " ▁ " + ( i + 6 ) ) ; } } public static void Main ( ) { int n = 25 ; printPrimeTriplets ( n ) ; } }