Find the sum of prime numbers in the Kth array
Last Updated :
13 Mar, 2022
Given K arrays where the first array contains the first prime number, the second array contains the next 2 primes and the third array contains the next 3 primes and so on. The task is to find the sum of primes in the Kth array.
Examples:
Input: K = 3
Output: 31
arr1[] = {2}
arr[] = {3, 5}
arr[] = {7, 11, 13}
7 + 11 + 13 = 31
Input: K = 2
Output: 8
Approach: Sieve of Eratosthenes can be used to find all the prime upto the required element. And the count of prime numbers in the arrays from 1 to K – 1 will be cnt = 1 + 2 + 3 + … + (K – 1) = (K * (K – 1)) / 2. Now, starting from the (cnt + 1)th prime from the sieve array, start adding all the primes until exactly K primes are added then print the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
bool prime[MAX];
void SieveOfEratosthenes()
{
for ( int i = 0; i < MAX; i++)
prime[i] = true ;
for ( int p = 2; p * p < MAX; p++) {
if (prime[p]) {
for ( int i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
int sumPrime( int k)
{
SieveOfEratosthenes();
vector< int > v;
for ( int i = 2; i < MAX; i++) {
if (prime[i])
v.push_back(i);
}
int sum = 0;
int skip = (k * (k - 1)) / 2;
while (k > 0) {
sum += v[skip];
skip++;
k--;
}
return sum;
}
int main()
{
int k = 3;
cout << sumPrime(k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 1000000 ;
static boolean []prime = new boolean [MAX];
static void SieveOfEratosthenes()
{
for ( int i = 0 ; i < MAX; i++)
prime[i] = true ;
for ( int p = 2 ; p * p < MAX; p++)
{
if (prime[p])
{
for ( int i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
static int sumPrime( int k)
{
SieveOfEratosthenes();
Vector<Integer> v = new Vector<>();
for ( int i = 2 ; i < MAX; i++)
{
if (prime[i])
v.add(i);
}
int sum = 0 ;
int skip = (k * (k - 1 )) / 2 ;
while (k > 0 )
{
sum += v.get(skip);
skip++;
k--;
}
return sum;
}
public static void main(String[] args)
{
int k = 3 ;
System.out.println(sumPrime(k));
}
}
|
Python3
from math import sqrt
MAX = 1000000
prime = [ True ] * MAX
def SieveOfEratosthenes() :
for p in range ( 2 , int (sqrt( MAX )) + 1 ) :
if (prime[p]) :
for i in range (p * p, MAX , p) :
prime[i] = False ;
def sumPrime(k) :
SieveOfEratosthenes();
v = [];
for i in range ( 2 , MAX ) :
if (prime[i]) :
v.append(i);
sum = 0 ;
skip = (k * (k - 1 )) / / 2 ;
while (k > 0 ) :
sum + = v[skip];
skip + = 1 ;
k - = 1 ;
return sum ;
if __name__ = = "__main__" :
k = 3 ;
print (sumPrime(k));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 1000000;
static bool []prime = new bool [MAX];
static void SieveOfEratosthenes()
{
for ( int i = 0; i < MAX; i++)
prime[i] = true ;
for ( int p = 2; p * p < MAX; p++)
{
if (prime[p])
{
for ( int i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
static int sumPrime( int k)
{
SieveOfEratosthenes();
List< int > v = new List< int >();
for ( int i = 2; i < MAX; i++)
{
if (prime[i])
v.Add(i);
}
int sum = 0;
int skip = (k * (k - 1)) / 2;
while (k > 0)
{
sum += v[skip];
skip++;
k--;
}
return sum;
}
public static void Main(String[] args)
{
int k = 3;
Console.WriteLine(sumPrime(k));
}
}
|
Javascript
<script>
const MAX = 1000000;
let prime = new Array(MAX);
function SieveOfEratosthenes()
{
for (let i = 0; i < MAX; i++)
prime[i] = true ;
for (let p = 2; p * p < MAX; p++) {
if (prime[p]) {
for (let i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
function sumPrime(k)
{
SieveOfEratosthenes();
let v = [];
for (let i = 2; i < MAX; i++) {
if (prime[i])
v.push(i);
}
let sum = 0;
let skip = parseInt((k * (k - 1)) / 2);
while (k > 0) {
sum += v[skip];
skip++;
k--;
}
return sum;
}
let k = 3;
document.write(sumPrime(k));
</script>
|
Time Complexity: O(MAX)
Auxiliary Space: O(MAX)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...