Sum of every K’th prime number in an array
Last Updated :
12 Sep, 2022
Given an integer k and an array of integers arr (less than 10^6), the task is to find the sum of every k’th prime number in the array.
Examples:
Input: arr[] = {2, 3, 5, 7, 11}, k = 2
Output: 10
All the elements of the array are prime. So, the prime numbers after every K (i.e. 2) interval are 3, 7 and their sum is 10.
Input: arr[] = {11, 13, 15, 17, 19}, k = 2
Output: 32
Simple Approach: Traverse the array and find every K’th prime number in the array and calculate the running sum. In this way, we’ll have to check every element of the array whether it is prime or not which will take more time as the size of the array increases.
Efficient Approach: Create a sieve that will store whether a number is prime or not. Then, it can be used to check a number against prime in O(1) time. In this way, we only have to keep track of every K’th prime number and maintain the running sum.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
bool prime[MAX + 1];
void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]"
// and initialize all the entries as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
memset(prime, true, sizeof(prime));
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= MAX; i += p)
prime[i] = false;
}
}
}
// compute the answer
void SumOfKthPrimes(int arr[], int n, int k)
{
// count of primes
int c = 0;
// sum of the primes
long long int sum = 0;
// traverse the array
for (int i = 0; i < n; i++) {
// if the number is a prime
if (prime[arr[i]]) {
// increase the count
c++;
// if it is the K'th prime
if (c % k == 0) {
sum += arr[i];
c = 0;
}
}
}
cout << sum << endl;
}
// Driver code
int main()
{
// create the sieve
SieveOfEratosthenes();
int arr[] = { 2, 3, 5, 7, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
SumOfKthPrimes(arr, n, k);
return 0;
}
Java
// Java implementation of the approach
public class GFG {
static int MAX = 1000000;
static boolean prime[] = new boolean[MAX + 1];
static void SieveOfEratosthenes() {
// Create a boolean array "prime[0..n]"
// and initialize all the entries as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
for (int i = 0; i < prime.length; i++) {
prime[i] = true;
}
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= MAX; i += p) {
prime[i] = false;
}
}
}
}
// compute the answer
static void SumOfKthPrimes(int arr[], int n, int k) {
// count of primes
int c = 0;
// sum of the primes
long sum = 0;
// traverse the array
for (int i = 0; i < n; i++) {
// if the number is a prime
if (prime[arr[i]]) {
// increase the count
c++;
// if it is the K'th prime
if (c % k == 0) {
sum += arr[i];
c = 0;
}
}
}
System.out.println(sum);
}
// Driver code
public static void main(String[] args) {
// create the sieve
SieveOfEratosthenes();
int arr[] = {2, 3, 5, 7, 11};
int n = arr.length;
int k = 2;
SumOfKthPrimes(arr, n, k);
}
}
Python3
# Python3 implementation of the approach
MAX = 100000;
prime = [True] * (MAX + 1);
def SieveOfEratosthenes():
# Create a boolean array "prime[0..n]"
# and initialize all the entries
# as true. A value in prime[i] will
# finally be false if i is Not a prime,
# else true.
# 0 and 1 are not prime numbers
prime[1] = False;
prime[0] = False;
p = 2;
while(p * p <= MAX):
# If prime[p] is not changed,
# then it is a prime
if (prime[p]):
# Update all multiples of p
i = p * 2;
while(i <= MAX):
prime[i] = False;
i += p;
p += 1;
# compute the answer
def SumOfKthPrimes(arr, n, k):
# count of primes
c = 0;
# sum of the primes
sum = 0;
# traverse the array
for i in range(n):
# if the number is a prime
if (prime[arr[i]]):
# increase the count
c+=1;
# if it is the K'th prime
if (c % k == 0):
sum += arr[i];
c = 0;
print(sum);
# Driver code
# create the sieve
SieveOfEratosthenes();
arr = [ 2, 3, 5, 7, 11 ];
n = len(arr);
k = 2;
SumOfKthPrimes(arr, n, k);
# This code is contributed by mits
C#
// C# implementation of the approach
class GFG
{
static int MAX = 1000000;
static bool[] prime = new bool[MAX + 1];
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]"
// and initialize all the entries as true.
// A value in prime[i] will finally be
// false if i is Not a prime, else true.
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p <= MAX; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == false)
{
// Update all multiples of p
for (int i = p * 2;
i <= MAX; i += p)
prime[i] = true;
}
}
}
// compute the answer
static void SumOfKthPrimes(int[] arr,
int n, int k)
{
// count of primes
int c = 0;
// sum of the primes
long sum = 0;
// traverse the array
for (int i = 0; i < n; i++)
{
// if the number is a prime
if (!prime[arr[i]])
{
// increase the count
c++;
// if it is the K'th prime
if (c % k == 0)
{
sum += arr[i];
c = 0;
}
}
}
System.Console.WriteLine(sum);
}
// Driver code
static void Main()
{
// create the sieve
SieveOfEratosthenes();
int[] arr = new int[] { 2, 3, 5, 7, 11 };
int n = arr.Length;
int k = 2;
SumOfKthPrimes(arr, n, k);
}
}
// This code is contributed by mits
PHP
<?php
// PHP implementation of the approach
$MAX = 100000;
$prime = array_fill(0, $MAX + 1, true);
function SieveOfEratosthenes()
{
global $MAX, $prime;
// Create a boolean array "prime[0..n]"
// and initialize all the entries
// as true. A value in prime[i] will
// finally be false if i is Not a prime,
// else true.
// 0 and 1 are not prime numbers
$prime[1] = false;
$prime[0] = false;
for ($p = 2; $p * $p <= $MAX; $p++)
{
// If prime[p] is not changed,
// then it is a prime
if ($prime[$p] == true)
{
// Update all multiples of p
for ($i = $p * 2;
$i <= $MAX; $i += $p)
$prime[$i] = false;
}
}
}
// compute the answer
function SumOfKthPrimes($arr, $n, $k)
{
global $MAX, $prime;
// count of primes
$c = 0;
// sum of the primes
$sum = 0;
// traverse the array
for ($i = 0; $i < $n; $i++)
{
// if the number is a prime
if ($prime[$arr[$i]])
{
// increase the count
$c++;
// if it is the K'th prime
if ($c % $k == 0)
{
$sum += $arr[$i];
$c = 0;
}
}
}
echo $sum."\n";
}
// Driver code
// create the sieve
SieveOfEratosthenes();
$arr = array( 2, 3, 5, 7, 11 );
$n = sizeof($arr);
$k = 2;
SumOfKthPrimes($arr, $n, $k);
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript implementation of the approach
let MAX = 100000;
let prime = new Array(MAX + 1).fill(true);
function SieveOfEratosthenes() {
// Create a boolean array "prime[0..n]"
// and initialize all the entries
// as true. A value in prime[i] will
// finally be false if i is Not a prime,
// else true.
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (let p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (let i = p * 2;
i <= MAX; i += p)
prime[i] = false;
}
}
}
// compute the answer
function SumOfKthPrimes(arr, n, k) {
// count of primes
let c = 0;
// sum of the primes
let sum = 0;
// traverse the array
for (let i = 0; i < n; i++) {
// if the number is a prime
if (prime[arr[i]]) {
// increase the count
c++;
// if it is the K'th prime
if (c % k == 0) {
sum += arr[i];
c = 0;
}
}
}
document.write(sum + "<br>");
}
// Driver code
// create the sieve
SieveOfEratosthenes();
let arr = new Array(2, 3, 5, 7, 11);
let n = arr.length;
let k = 2;
SumOfKthPrimes(arr, n, k);
// This code is contributed by gfgking
</script>
Complexity Analysis:
- Time Complexity: O(n + MAX2)
- Auxiliary Space: O(MAX)
Similar Reads
Sum of every K'th prime number in an array Given an array of integers (less than 10^6), the task is to find the sum of all the prime numbers which appear after every (k-1) prime number i.e. every K'th prime number in the array. Examples: Input : Array : 2, 3, 5, 7, 11 ; n=5; k=2 Output : Sum = 10 Explanation: All the elements of the array ar
7 min read
Product of every Kâth prime number in an array Given an integer 'k' and an array of integers 'arr' (less than 10^6), the task is to find the product of every K'th prime number in the array. Examples: Input: arr = {2, 3, 5, 7, 11}, k = 2 Output: 21 All the elements of the array are prime. So, the prime numbers after every K (i.e. 2) interval are
12 min read
Sum of all prime numbers in an Array Given an array arr[] of N positive integers. The task is to write a program to find the sum of all prime elements in the given array. Examples: Input: arr[] = {1, 3, 4, 5, 7} Output: 15 There are three primes, 3, 5 and 7 whose sum =15. Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 17 Naive Approach:
7 min read
Find the sum of prime numbers in the Kth array 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,
9 min read
Count number of primes in an array Given an array arr[] of N positive integers. The task is to write a program to count the number of prime elements in the given array. Examples: Input: arr[] = {1, 3, 4, 5, 7} Output: 3 There are three primes, 3, 5 and 7 Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Naive Approach: A simple solution
8 min read
Sum of Maximum and Minimum prime factor of every number in the Array Given an array arr[], the task is to find the sum of the maximum and the minimum prime factor of every number in the given array.Examples: Input: arr[] = {15} Output: 8 The maximum and the minimum prime factors of 15 are 5 and 3 respectively.Input: arr[] = {5, 10, 15, 20, 25, 30} Output: 10 7 8 7 10
9 min read
Nearest prime number in the array of every array element Given an integer array arr[] consisting of N integers, the task is to find the nearest Prime Number in the array for every element in the array. If the array does not contain any prime number, then print -1. Examples: Input: arr[] = {1, 2, 3, 1, 6} Output: 2 2 3 3 3 Explanation: For the subarray {1,
11 min read
Print prime numbers with prime sum of digits in an array Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[]
8 min read
Sum of the first N Prime numbers Given an integer 'n', the task is to find the sum of first 'n' prime numbers. First few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, ...... Examples: Input: N = 4Output: 172, 3, 5, 7 are first 4 prime numbers so their sum is equal to 17Input: N = 40Output: 3087 Approach: Create a sieve which w
7 min read
Sum of all the prime divisors of a number Given a number N. The task is to find the sum of all the prime divisors of N. Examples: Input: 60Output: 102, 3, 5 are prime divisors of 60Input: 39Output: 163, 13 are prime divisors of 39A naive approach will be to iterate for all numbers till N and check if the number divides N. If the number divi
15+ min read