
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Count Prime Numbers in an Array
Prime numbers are natural numbers greater than 1 with only two factors: 1 and themselves. Examples include 2, 3, 5, 7, etc.
The smallest prime number is 2.
Problem Description
We are given an array and need to find the number of prime numbers in the array using C#. Below is an example to understand the problem:
Examples
Input: array = { 3, 6, 12, 7, 9, 11}
Output: array = {3, 7, 11}
Explanation: The numbers 3, 7, and 11 have only two factors: 1 and themselves, so they are prime numbers.
Input: array = {4, 6, 19, 23, 33}
Output: array = {19, 23}
Explanation: The numbers 19 and 23 are prime numbers as they have only two factors: 1 and themselves.
Count Primes in Array Using Division Approach
Numbers less than or equal to 1 are not prime. Using the division approach, we check if a number has any divisors other than 1 and itself. If it does, it's not a prime number. The check is performed up to ?n as factors repeat beyond ?n.
Steps for Implementation
- Create a function to check if a number is prime.
- Loop from 2 to ?number; if a divisor is found, return false.
- If no divisors are found, return true.
- Call this function for each number in the array.
- Count the numbers that are prime.
Implementation Code
using System; class PrimeCountOptimized { static bool checkPrime(int num) { if (num <= 1) return false; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true; } static int CountPrimes(int[] arr) { int count = 0; foreach (int num in arr) { if (checkPrime(num)) count++; } return count; } static void Main() { int[] array = { 3, 6, 12, 7, 9, 11 }; int totalPrimes = CountPrimes(array); Console.WriteLine("Total number of prime numbers in the array: " + totalPrimes); } }
Output:
Total number of prime numbers in the array: 3
Time Complexity: O(n * ?m), where n is the number of elements in the array and m is the largest number in the array.
Space Complexity: O(1) (constant space).
Count Primes in Array Using Sieve of Eratosthenes Approach
The Sieve of Eratosthenes efficiently finds all prime numbers up to a given number n. It marks multiples of each prime as non-prime in a boolean array.
Steps for Implementation
- Create a boolean array `isPrime` of size (max number in array + 1).
- Mark 0 and 1 as non-prime.
- Iterate from 2 to ?max and mark multiples of each prime as non-prime.
- For each number in the input array, check its value in `isPrime` and increase the count if true.
Implementation Code
using System; using System.Linq; class PrimeCountSieve { static int UsingSieveOfEratosthenes(int[] arr) { if (arr.Length == 0) return 0; int max = arr.Max(); if (max < 2) return 0; bool[] isPrime = new bool[max + 1]; Array.Fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= max; i++) { if (isPrime[i]) { for (int j = i * i; j <= max; j += i) isPrime[j] = false; } } int count = 0; foreach (int num in arr) { if (num > 1 && isPrime[num]) count++; } return count; } static void Main() { int[] array = { 3, 6, 12, 7, 9, 11 }; int primeCount = UsingSieveOfEratosthenes(array); Console.WriteLine("Total number of primes in the array: " + primeCount); } }
Output:
Total number of primes in the array: 3
Time Complexity: O(max * log(log(max))) + O(n)
Space Complexity: O(max) (maximum number in input array).
Which Approach to Use?
Use the division approach for small arrays or limited memory. Use the Sieve of Eratosthenes for large arrays with high numbers, though it uses more memory.