Find the sum of prime numbers in the Kth array | Python3 implementation of the approach ; Function for 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 greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the sum of primes in the Kth array ; Update vector v to store all the prime numbers upto MAX ; To store the sum of primes in the kth array ; Count of primes which are in the arrays from 1 to k - 1 ; k is the number of primes in the kth array ; A prime has been added to the sum ; Driver code
from math import sqrt NEW_LINE MAX = 1000000 NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 ) : NEW_LINE DEDENT prime = [ True ] * MAX NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * p , MAX , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT def sumPrime ( k ) : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE v = [ ] ; NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT v . append ( i ) ; NEW_LINE DEDENT DEDENT sum = 0 ; NEW_LINE skip = ( k * ( k - 1 ) ) // 2 ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT sum += v [ skip ] ; NEW_LINE skip += 1 ; NEW_LINE k -= 1 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 ; NEW_LINE print ( sumPrime ( k ) ) ; NEW_LINE DEDENT