原文:

给定一个数组 arr[] ,任务是打印数组中的加法素数。 加性素数:这样的素数,它们的位数之和也是素数,如 2、3、7、11、23 是加性素数但不是 13、19、31 等。 举例:

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

一种简单的方法是遍历所有的数组元素。对于每个元素,检查它是否是加法素数。 当数组很小或数组值很大时,上述方法可以。对于数值相对较小的大型数组,我们使用筛选来存储最大数组元素的素数。然后检查当前元素是否是质数。如果是,那么检查它的数字之和是否也是质数。如果是,则打印该号码。 以下是上述方法的实施:

c

// c   implementation of the above approach
#include 
using namespace std;
// function to store the primes
void 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 digits
int digitsum(int n)
{
    int sum = 0;
    while (n) {
        sum  = n % 10;
        n = n / 10;
    }
    return sum;
}
// function to print additive primes
void 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 code
int 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 approach
import java.util.arrays;
class gfg
{
// function to store the primes
static 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 digits
static int digitsum(int n)
{
    int sum = 0;
    while (n > 0)
    {
        sum  = n % 10;
        n = n / 10;
    }
    return sum;
}
// function to print additive primes
static 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 code
public 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

python 3

# python3 implementation of the
# above approach
# from math lib import sqrt
from 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 primes
def 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 approach
using system.linq;
using system;
class gfg
{
// function to store the primes
static 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 digits
static int digitsum(int n)
{
    int sum = 0;
    while (n > 0)
    {
        sum  = n % 10;
        n = n / 10;
    }
    return sum;
}
// function to print additive primes
static 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 code
static 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

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

2 11 7