Find the largest twins in given range | Function to find twins ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the largest twin in range ; Driver Code
from math import sqrt , floor NEW_LINE def printTwins ( low , high ) : NEW_LINE INDENT prime = [ True for i in range ( high + 1 ) ] NEW_LINE twin = False NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE k = floor ( sqrt ( high ) ) + 2 NEW_LINE for p in range ( 2 , k , 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , high + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT i = high NEW_LINE while ( i >= low ) : NEW_LINE INDENT if ( prime [ i ] and ( i - 2 >= low and prime [ i - 2 ] == True ) ) : NEW_LINE INDENT print ( " Largest ▁ twins ▁ in ▁ given ▁ range : ( " , ( i - 2 ) , " , " , ( i ) , " ) " ) NEW_LINE twin = True NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( twin == False ) : NEW_LINE INDENT print ( " No ▁ such ▁ pair ▁ exists " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT printTwins ( 10 , 100 ) NEW_LINE DEDENT