Sum of every Kâ €™ th prime number in an array | PHP implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; create the sieve
< ? php $ MAX = 100000 ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; $ prime [ 1 ] = false ; $ prime [ 0 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function SumOfKthPrimes ( $ arr , $ n , $ k ) { global $ MAX , $ prime ; $ c = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) { $ c ++ ; if ( $ c % $ k == 0 ) { $ sum += $ arr [ $ i ] ; $ c = 0 ; } } } echo $ sum . " STRNEWLINE " ; } SieveOfEratosthenes ( ) ; $ arr = array ( 2 , 3 , 5 , 7 , 11 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; SumOfKthPrimes ( $ arr , $ n , $ k ) ; ? >