Product of every K’th prime number in an array in C++

C++Server Side ProgrammingProgramming

Given an array arr[n] containing n prime numbers and k; the task is to find the product of every k’th prime number in an array.

Like, we have an array arr[] = {3, 5, 7, 11} and k = 2 so the prime number after every k i.e 5 and 11 we have to find their product which will be 5x11 = 55 and print the result as output.

What are prime numbers?

A prime number is a natural number which can’t be divided by any other number except 1 or the number itself. Some of the prime numbers are 2, 3, 5, 7, 11, 13 etc.

Example

Input: arr[] = {3, 5, 7, 11, 13} k= 2
Output: 55
Explanation: every 2nd element of the array are 5 and 11; their product will be 55

Input: arr[] = {5, 7, 13, 23, 31} k = 3
Output: 13
Explanation: every 3rd element of an array is 13 so the output will be 13.

Approach we will be using to solve the above problem

  • Take an input array of n elements and k, for finding product of every k’th element.
  • Create a sieve for storing the prime numbers.
  • Then we have to traverse the array and get the k’th element and multiply it with the product variable recursively for every k’th element.
  • Print the product.

Algorithm

Start
Step 1-> Define and initialize MAX 1000000
Step 2-> Define bool prime[MAX + 1]
Step 3-> In function createsieve()
   Call memset(prime, true, sizeof(prime));
   Set prime[1] = false
   Set prime[0] = false
   Loop For p = 2 and p * p <= MAX and p++
      If prime[p] == true then,
         For i = p * 2 and i <= MAX and i += p
         Set prime[i] = false
Step 4-> void productOfKthPrimes(int arr[], int n, int k)
   Set c = 0
   Set product = 1
   Loop For i = 0 and i < n and i++
   If prime[arr[i]] then,
      Increment c by 1
      If c % k == 0 {
         Set product = product * arr[i]
         Set c = 0
      Print the product
Step 5-> In function main()
   Call function createsieve()
   Set n = 5, k = 2
   Set arr[n] = { 2, 3, 11, 13, 23 }
   Call productOfKthPrimes(arr, n, k)
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
bool prime[MAX + 1];
void createsieve() {
   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] == true) {
         // finding all multiples of p
         for (int i = p * 2; i <= MAX; i += p)
         prime[i] = false;
      }
   }
}
// compute the answer
void productOfKthPrimes(int arr[], int n, int k) {
   // count the number of primes
   int c = 0;
   // find the product of the primes
   long long int product = 1;
   // traverse the array
   for (int i = 0; i < n; i++) {
      // if the number is a prime
      if (prime[arr[i]]) {
         c++;
         if (c % k == 0) {
            product *= arr[i];
            c = 0;
         }
      }
   }
   cout << product << endl;
}
//main block
int main() {
   // create the sieve
   createsieve();
   int n = 5, k = 2;
   int arr[n] = { 2, 3, 11, 13, 23 };
   productOfKthPrimes(arr, n, k);  
   return 0;
}

Output

39
raja
Published on 23-Dec-2019 11:04:17
Advertisements