Count numbers which can be represented as sum of same parity primes
Given an arr[] of positive integers you have to count how many numbers can be represented as sum of same parity prime numbers(can be same)
Examples:
Input : arr[] = {1, 3, 4, 6}
Output : 2
4 = 2+2, 6 = 3+3
Input : arr[] = {4, 98, 0, 36, 51}
Output : 31. If two numbers of same parity are added then they would be always even, so all odd numbers in the array can never contribute to answer.
2. Talking about 0 and 2 both cannot be represented by sum of same parity prime numbers.
3. Rest of all numbers will contribute to the answer (Refer https://www.geeksforgeeks.org/program-for-goldbachs-conjecture-two-primes-with-given-sum/)
So, we have to just iterate over the entire array and find out number of even elements not equal to 0 and 2.
C++
#include <bits/stdc++.h>using namespace std;// Function to calculate countint 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 Codeint 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 primesimport java.util.*;class GFG{// Function to calculate countpublic 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 codepublic 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 countdef 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 Codeif __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 primesusing System;class GFG{// Function to calculate countpublic 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 codestatic 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
<?php// Function to calculate countfunction calculate(&$array, $size){ $count = 0; for ($i = 0; $i < $size; $i++) if ($array[$i] % 2 == 0 && $array[$i] != 0 && $array[$i] != 2) $count++; return $count;}// Driver Code$a = array(1, 3, 4, 6 );$size = sizeof($a);echo calculate($a, $size);// This code is contributed// by ChitraNayal?> |
Javascript
<script>// Javascript program to Count numbers// which can be represented as// sum of same parity primes// Function to calculate countfunction calculate(ar, size){ var count = 0; for(i = 0; i < size; i++) if (ar[i] % 2 == 0 && ar[i] != 0 && ar[i] != 2) count++; return count;}// Driver codevar a = [ 1, 3, 4, 6 ];var size = a.length;document.write(calculate(a, size));// This code is contributed by todaysgaurav</script> |
2
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


