Prime triplets consisting of values up to N having difference between two elements equal to the third | Python3 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 ;  ; Iterate over the range [ 3 , N ] ; Check for the condition ; Store the triplets ; Print all the stored triplets ; Driver Code
from math import sqrt NEW_LINE prime = [ True for i in range ( 100000 ) ] NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def findTriplets ( N ) : NEW_LINE INDENT SieveOfEratosthenes ( N ) NEW_LINE DEDENT / * List < List < int > > V = new List < List < int >> ( ) ; * / NEW_LINE INDENT for i in range ( 3 , N + 1 , 1 ) : NEW_LINE INDENT if ( 2 + i <= N and prime [ i ] and prime [ 2 + i ] ) : NEW_LINE INDENT V . append ( [ 2 , i , i + 2 ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( V ) ) : NEW_LINE INDENT print ( V [ i ] [ 0 ] , V [ i ] [ 1 ] , V [ i ] [ 2 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE findTriplets ( N ) NEW_LINE DEDENT