Find the smallest twins in given range | Javascript program to find the smallest twin in given range ; 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 prvar the smallest twin in range ; Driver code
function printTwins ( low , high ) { var prime = Array . from ( { length : high + 1 } , ( _ , i ) => 0 ) ; var twin = false ; for ( i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( i = p * 2 ; i <= high ; i += p ) { prime [ i ] = false ; } } } for ( i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { var a = i + 2 ; document . write ( " " + " " + i + " " + a + " " ) ; twin = true ; break ; } } if ( twin == false ) { document . write ( " " ) ; } } printTwins ( 10 , 100 ) ;