Count and Sum of composite elements in an array in C++

C++Server Side ProgrammingProgramming

We are given with an array of positive integers and the task is to calculate the count and sum of the composite elements in the given array.

What are composite numbers

From the given set of integers, the numbers that are not prime are called composite numbers except 1 which is neither composite nor prime instead it’s a unit number. So, it is clearly stated that a number can be either prime or composite except the number 1.

The composite upto 100 are given below −

4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 
26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 
45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 
63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 
81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 
98, 99, 100

For Example

Input − array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Output − total count of composite numbers is: 5
      Sum of composite number is: 37

Explanation − 4, 6, 8, 9, 10 are the composite numbers present in a given array. So, their count is 5 and their sum is 4+6+8+9+10 = 37

Input − array[] = {1, 2, 3, 4, 5}
Output − total count of composite numbers is: 1
      Sum of composite number is: 4

Explanation − 4 is the only composite number present in a given array. So, their count is 1 and their sum is 4

Approach used in the below program is as follows &miuns;

  • Input the array of positive integers

  • Calculate its size

  • Initialise the variable sum to store the sum of composite numbers

  • Store the maximum value present in an array in a variable

  • Calculate the prime numbers till the maximum value

  • Traverse the entire array and check for whether the number is prime or not. If the number isn’t prime then it will be composite number and if it is so, then increase the count for composite number by 1 and add its value to the sum

Example

 Live Demo

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to find and return the
// the count of the composite numbers
int compcount(int ar[], int num, int* sum){
   // storing the largest element of the array
   int max_val = *max_element(ar, ar + num);
   // Using sieve to find all prime numbers
   // less than or equal to max_val
   // Create a boolean array "prime[0..n]". A
   // value in prime[i] will finally be false
   vector<bool> pr(max_val + 1, true);
   // setting the values of 0 and 1 as
   // true for prime.
   pr[0] = true;
   pr[1] = true;
   for (int p = 2; p * p <= max_val; p++){
      // If prime[p] is not changed, then
      // it is a prime
      if (pr[p] == true){
         // Update all multiples of p
         for (int i = p * 2; i <= max_val; i += p){
            pr[i] = false;
         }
      }
   }
   // Count all composite
   // numbers in the arr[]
   int ans = 0;
   for (int i = 0; i < num; i++){
      if (!pr[ar[i]]){
         ans++;
         *sum = *sum + ar[i];
      }
   }
   return ans;
}
// Driver code
int main(){
   int ar[] = { 1, 2, 3, 4, 5 };
   int num = sizeof(ar) / sizeof(ar[0]);
   int sum = 0;
   cout << "Count of Composite Numbers = "<< compcount(ar, num, &sum);
   cout << "\nSum of Composite Numbers = " << sum;
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of Composite Numbers = 1
Sum of Composite Numbers = 4
raja
Published on 15-May-2020 12:40:33
Advertisements