给定arr []为正整数,您必须计算多少个数字可以表示为相同奇偶素数的和(可以相同)
例子:
Input : arr[] = {1, 3, 4, 6}
Output : 2
4 = 2+2, 6 = 3+3
Input : arr[] = {4, 98, 0, 36, 51}
Output : 3
1.如果将两个具有相同奇偶校验的数字相加,则它们将始终为偶数,因此数组中的所有奇数都将永远无法构成答案。
2.谈论0和2都不能用相同的奇偶素数之和表示。
3.其余所有数字将有助于答案(请参阅https://www.geeksforgeeks.org/program-for-goldbachs-conjecture-two-primes-with-given-sum/)
因此,我们必须遍历整个数组并找出不等于0和2的偶数元素的数量。
C++
#include
using namespace std;
// Function to calculate count
int calculate(int* array, int size)
{
int count = 0;
for (int i = 0; i < size; i++)
if (array[i] % 2 == 0 &&
array[i] != 0 &&
array[i] != 2)
count++;
return count;
}
// Driver Code
int main()
{
int a[] = { 1, 3, 4, 6 };
int size = sizeof(a) / sizeof(a[0]);
cout << calculate(a, size);
}
Java
// Java program to Count numbers
// which can be represented as
// sum of same parity primes
import java.util.*;
class GFG
{
// Function to calculate count
public static int calculate(int ar[],
int size)
{
int count = 0;
for (int i = 0; i < size; i++)
if (ar[i] % 2 == 0 &&
ar[i] != 0 &&
ar[i] != 2)
count++;
return count;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 1, 3, 4, 6 };
int size = a.length;
System.out.print(calculate(a, size));
}
}
// This code is contributed
// by ankita_saini
Python3
# Function to calculate count
def calculate(array, size):
count = 0
for i in range(size):
if (array[i] % 2 == 0 and
array[i] != 0 and
array[i] != 2 ):
count += 1
return count
# Driver Code
if __name__ == "__main__":
a = [ 1, 3, 4, 6 ]
size = len(a)
print(calculate(a, size))
# This code is contributed
# by ChitraNayal
C#
// C# program to Count numbers
// which can be represented as
// sum of same parity primes
using System;
class GFG
{
// Function to calculate count
public static int calculate(int []ar,
int size)
{
int count = 0;
for (int i = 0; i < size; i++)
if (ar[i] % 2 == 0 &&
ar[i] != 0 &&
ar[i] != 2)
count++;
return count;
}
// Driver code
static public void Main (String []args)
{
int []a = { 1, 3, 4, 6 };
int size = a.Length;
Console.WriteLine(calculate(a, size));
}
}
// This code is contributed
// by Arnab Kundu
PHP
Javascript
输出:
2