C program to check the number of prime numbers pairs in an array
Answers
Answer:
Check the number of prime numbers pairs in an array
Explanation:
using System;
using System.Linq;
class GFI {
// find prime pairs
static int pairCount(int[] arr, int a)
{
//To Find max value in the array
int maximum_val = arr.Maximum();
// ALL PRIME NUMBERS LESS THAN OR EQUAL TO maximum_val
// Create a boolean array "prime[0..a]"
.
bool[] prime = new bool[maximum_val + 1];
for (int i = 0; i < maximum_val + 1; i++) {
prime[i] = true;
}
// Remaining part of SIEVE
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= maximum_val; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= maximum_val; i += p) {
prime[i] = false;
}
}
}
// Find all primes in arr[]
int count = 0;
for (int i = 0; i < a; i++) {
if (prime[arr[i]]) {
count++;
}
}
// return
// Prime pairs
// Unique pairs
// with N elements is N*(N-1)/2
return (count * (count - 1)) / 2;
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
int a = arr.Length;
Console.WriteLine(pairCount(arr, a));
}
Main Loop:
...
for(i = 0; i < size; i++)
{
if(arr[i] == 2)
{
primeNum[k++] = arr[i];
}
else
{
isPrime = 1;
for(j = 2; j < arr[i]; j++)
{
if(arr[i] % j == 0)
{
isPrime = 0;
break;
}
}
if(isPrime == 1)
{
primeNum[k++] = arr[i];
}
}
}
...
Similar Question: Take 5 numbers in an array and find all pairs of prime numbers from that array