text: Find count of Almost Prime numbers from 1 to N | PHP program to count almost prime numbers from 1 to n ; 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 ; Function to count almost prime numbers from 1 to n ; to store required answer ; 6 is first almost prime number ; to count prime factors ; if it is perfect square ; if I is almost prime number ; Driver Code
code: < ? php $ N = 100005 ; $ prime = array_fill ( 0 , $ N , true ) ; function SieveOfEratosthenes ( ) { global $ N , $ prime ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p < ( int ) ( sqrt ( $ N ) ) ; $ p ++ ) { if ( $ prime [ $ p ] == true ) for ( $ i = 2 * $ p ; $ i < $ N ; $ i += $ p ) $ prime [ $ i ] = false ; } } function almostPrimes ( $ n ) { global $ prime ; $ ans = 0 ; for ( $ i = 6 ; $ i < $ n + 1 ; $ i ++ ) { $ c = 0 ; for ( $ j = 2 ; $ i >= $ j * $ j ; $ j ++ ) { if ( $ i % $ j == 0 ) { if ( $ j * $ j == $ i ) { if ( $ prime [ $ j ] ) $ c += 1 ; } else { if ( $ prime [ $ j ] ) $ c += 1 ; if ( $ prime [ ( $ i / $ j ) ] ) $ c += 1 ; } } } if ( $ c == 2 ) $ ans += 1 ; } return $ ans ; } SieveOfEratosthenes ( ) ; $ n = 21 ; print ( almostPrimes ( $ n ) ) ; ? >