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
function printTwins ( low , high ) { let prime = new Array ( high + 1 ) ; let twin = false ; for ( let i = 0 ; i < high + 1 ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= high ; i += p ) prime [ i ] = false ; } } for ( let i = high ; i >= low ; i -- ) { if ( prime [ i ] && ( i - 2 >= low && prime [ i - 2 ] == true ) ) { document . write ( " " + ( i - 2 ) + " " + ( i ) + " " + " " ) ; twin = true ; break ; } } if ( twin == false ) document . write ( " " ) ; } printTwins ( 10 , 100 ) ;