Twin Prime Numbers between 1 and n | Python program to illustrate ... To print total number of twin prime using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; check twin prime numbers display the twin prime numbers ; driver program ; Calling the function
def printTwinPrime ( n ) : NEW_LINE INDENT prime = [ True for i in range ( n + 2 ) ] NEW_LINE p = 2 NEW_LINE while ( p * p <= n + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 2 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , n - 1 ) : NEW_LINE INDENT if prime [ p ] and prime [ p + 2 ] : NEW_LINE INDENT print ( " ( " , p , " , " , ( p + 2 ) , " ) " , end = ' ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 25 NEW_LINE printTwinPrime ( n ) NEW_LINE DEDENT