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[] = {2, 3, 19, 13, 25, 7}
Output: 2, 3, 7
A simple approach is to traverse through all array elements. For every element check if it is Additive prime or not.
This above approach is fine when array is small or when array values are large. For large sized arrays having relatively small values, we use Sieve to store primes up to maximum element of the array. Then check if the current element is prime or not. If yes then check the sum of its digit is also prime or not. If yes then print that number.
Algorithm:
Step 1: Start
Step 2: Create the function sieve(), which accepts a prime[] array and maxEle (the array’s maximum element) as inputs.
a. As they are not primes, set prime[0] and prime[1] to 1.
b. Iterate from 2 to the square root of maxEle using a for a loop. If the current index I is not a prime number, use another for loop to set prime[j] to 1 for each j that is a multiple of I marking all of its multiples as not primes.
Step 3: Establish the digitSum() function, which receives an integer n as input and returns the sum of its digits.
Step 4: Create the function printAdditivePrime(), which accepts as input an integer array arr[] of size n.
a. Use the max element() function from the algorithm library to determine the maximum element maxEle in arr[].
b. Use memset to declare an integer array prime[] with a size of maxEle + 1 and initialize it to all 0s ().
c. To add 0s and 1s to the prime[] array, use the sieve() function.
d. Iterate through each element of arr[ using a for a loop.
e. If the current element is a prime (prime[arr[i]] == 0), use the digitSum() function to get its digit sum.
f. Check if the sum of the digits is a prime number (prime[sum] == 0). Print the current element if it is.
Step 5: End
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;// Function to store the primesvoid sieve(int maxEle, int prime[]){ prime[0] = prime[1] = 1; for (int i = 2; i * i <= maxEle; i++) { if (!prime[i]) { for (int j = 2 * i; j <= maxEle; j += i) prime[j] = 1; } }}// Function to return the sum of digitsint digitSum(int n){ int sum = 0; while (n) { sum += n % 10; n = n / 10; } return sum;}// Function to print additive primesvoid printAdditivePrime(int arr[], int n){ int maxEle = *max_element(arr, arr + n); int prime[maxEle + 1]; memset(prime, 0, sizeof(prime)); sieve(maxEle, prime); for (int i = 0; i < n; i++) { // If the number is prime if (prime[arr[i]] == 0) { int sum = digitSum(arr[i]); // Check if it's digit sum is prime if (prime[sum] == 0) cout << arr[i] << " "; } }}// Driver codeint main(){ int a[] = { 2, 4, 6, 11, 12, 18, 7 }; int n = sizeof(a) / sizeof(a[0]); printAdditivePrime(a, n); return 0;} |
Java
// Java implementation of the above approachimport java.util.Arrays;class GFG{ // Function to store the primesstatic void sieve(int maxEle, int prime[]){ prime[0] = prime[1] = 1; for (int i = 2; i * i <= maxEle; i++) { if (prime[i]==0) { for (int j = 2 * i; j <= maxEle; j += i) prime[j] = 1; } }}// Function to return the sum of digitsstatic int digitSum(int n){ int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum;}// Function to print additive primesstatic void printAdditivePrime(int arr[], int n){ int maxEle = Arrays.stream(arr).max().getAsInt(); int prime[] = new int[maxEle + 1]; sieve(maxEle, prime); for (int i = 0; i < n; i++) { // If the number is prime if (prime[arr[i]] == 0) { int sum = digitSum(arr[i]); // Check if it's digit sum is prime if (prime[sum] == 0) System.out.print(arr[i]+" "); } }}// Driver codepublic static void main(String[] args){ int a[] = { 2, 4, 6, 11, 12, 18, 7 }; int n =a.length; printAdditivePrime(a, n);}}// This code is contributed by chandan_jnu |
Python3
# Python3 implementation of the # above approach # from math lib import sqrtfrom math import sqrt# Function to store the primes def sieve(maxEle, prime) : prime[0], prime[1] = 1 , 1 for i in range(2, int(sqrt(maxEle)) + 1) : if (not prime[i]) : for j in range(2 * i , maxEle + 1, i) : prime[j] = 1 # Function to return the sum of digits def digitSum(n) : sum = 0 while (n) : sum += n % 10 n = n // 10 return sum# Function to print additive primesdef printAdditivePrime(arr, n): maxEle = max(arr) prime = [0] * (maxEle + 1) sieve(maxEle, prime) for i in range(n) : # If the number is prime if (prime[arr[i]] == 0): sum = digitSum(arr[i]) # Check if it's digit sum is prime if (prime[sum] == 0) : print(arr[i], end = " ") # Driver code if __name__ == "__main__" : a = [ 2, 4, 6, 11, 12, 18, 7 ] n = len(a) printAdditivePrime(a, n) # This code is contributed by Ryuga |
C#
// C# implementation of the above approachusing System.Linq;using System;class GFG{ // Function to store the primesstatic void sieve(int maxEle, int[] prime){ prime[0] = prime[1] = 1; for (int i = 2; i * i <= maxEle; i++) { if (prime[i] == 0) { for (int j = 2 * i; j <= maxEle; j += i) prime[j] = 1; } }}// Function to return the sum of digitsstatic int digitSum(int n){ int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum;}// Function to print additive primesstatic void printAdditivePrime(int []arr, int n){ int maxEle = arr.Max(); int[] prime = new int[maxEle + 1]; sieve(maxEle, prime); for (int i = 0; i < n; i++) { // If the number is prime if (prime[arr[i]] == 0) { int sum = digitSum(arr[i]); // Check if it's digit sum is prime if (prime[sum] == 0) Console.Write(arr[i] + " "); } }}// Driver codestatic void Main(){ int[] a = { 2, 4, 6, 11, 12, 18, 7 }; int n = a.Length; printAdditivePrime(a, n);}}// This code is contributed by chandan_jnu |
PHP
<?php// PHP implementation of the above approach// Function to store the primesfunction sieve($maxEle, &$prime){ $prime[0] = $prime[1] = 1; for ($i = 2; $i * $i <= $maxEle; $i++) { if (!$prime[$i]) { for ($j = 2 * $i; $j <= $maxEle; $j += $i) $prime[$j] = 1; } }}// Function to return the sum of digitsfunction digitSum($n){ $sum = 0; while ($n) { $sum += $n % 10; $n = $n / 10; } return $sum;}// Function to print additive primesfunction printAdditivePrime($arr, $n){ $maxEle = max($arr); $prime = array_fill(0, $maxEle + 1, 0); sieve($maxEle, $prime); for ($i = 0; $i < $n; $i++) { // If the number is prime if ($prime[$arr[$i]] == 0) { $sum = digitSum($arr[$i]); // Check if it's digit sum is prime if ($prime[$sum] == 0) print($arr[$i] . " "); } }}// Driver code$a = array(2, 4, 6, 11, 12, 18, 7);$n = count($a);printAdditivePrime($a, $n);// This code is contributed by chandan_jnu?> |
Javascript
<script>// Javascript implementation of the above approach// Function to store the primesfunction sieve(maxEle, prime){ prime[0] = prime[1] = 1; for (var i = 2; i * i <= maxEle; i++) { if (!prime[i]) { for (var j = 2 * i; j <= maxEle; j += i) prime[j] = 1; } }}// Function to return the sum of digitsfunction digitSum(n){ var sum = 0; while (n) { sum += n % 10; n = parseInt(n / 10); } return sum;}// Function to print additive primesfunction printAdditivePrime(arr, n){ var maxEle = arr.reduce((a,b)=> Math.max(a,b)); var prime = Array(maxEle + 1).fill(0); sieve(maxEle, prime); for (var i = 0; i < n; i++) { // If the number is prime if (prime[arr[i]] == 0) { var sum = digitSum(arr[i]); // Check if it's digit sum is prime if (prime[sum] == 0) document.write( arr[i] + " "); } }}// Driver codevar a = [ 2, 4, 6, 11, 12, 18, 7 ];var n = a.length;printAdditivePrime(a, n);</script> |
2 11 7
Time Complexity: O(max*log(log(max))) where max is the maximum element in the array.
Auxiliary Space: O(maxEle), where maxEle is the largest element of the array a.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



