Alternate Primes till N | PHP program to print all primes smaller than or equal to n 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 ; Print all prime numbers ; for next prime to get printed ; Driver Code
< ? php function SieveOfEratosthenes ( $ n ) { $ prime = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ prime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ flag = true ; for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { if ( $ flag ) { echo $ p . " " ; $ flag = false ; } else { $ flag = true ; } } } } $ n = 15 ; echo " Following ▁ are ▁ the ▁ alternate " . " ▁ prime ▁ numbers ▁ smaller ▁ " . " than ▁ or ▁ equal ▁ to ▁ " . $ n . " STRNEWLINE " ; SieveOfEratosthenes ( $ n ) ; ? >