Find the smallest twins in given range | PHP 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 print the smallest twin in range ; Driver Code
< ? php function printTwins ( $ low , $ high ) { $ prime = array_fill ( 0 , $ high + 1 , true ) ; $ twin = false ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p <= floor ( 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 ] ) { print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( $ i , ▁ " . ( $ i + 2 ) . " ) " ) ; $ twin = true ; break ; } } if ( $ twin == false ) print ( " No ▁ such ▁ pair ▁ exists STRNEWLINE " ) ; } printTwins ( 10 , 100 ) ; ? >