Skip to content

Instantly share code, notes, and snippets.

@mcmullm2-dcu
Created February 4, 2018 10:05
Show Gist options
  • Save mcmullm2-dcu/648f08f3c4e96e368cbf7f4c47e0cb74 to your computer and use it in GitHub Desktop.
Save mcmullm2-dcu/648f08f3c4e96e368cbf7f4c47e0cb74 to your computer and use it in GitHub Desktop.
C# implementation of the Sieve of Eratosthenes, an algorithm to calculate prime numbers.
using System;
/// Implementation of the sieve of Eratosthenes for finding all the primes up to
/// a given number (MAX in this case).
/// From the command line:
/// Step 1 (Compile): csc PrimesSieve.cs
/// Step 2 (Run): .\PrimesSieve
public class PrimesSieve {
static void Main() {
const int MAX = 1000000;
// Create an array of boolean values indicating whether a number is prime.
// Start by assuming all numbers are prime by setting them to true.
bool[] primes = new bool[MAX + 1];
for (int i=0; i<primes.Length; i++) {
primes[i] = true;
}
// Loop through a portion of the array (up to the square root of MAX). If
// it's a prime, ensure all multiples of it are set to false, as they
// clearly cannot be prime.
for (int i=2; i<Math.Sqrt(MAX)+1; i++) {
if (primes[i-1]) {
for (int j=(int) Math.Pow(i,2); j<=MAX; j+=i) {
primes[j - 1] = false;
}
}
}
// Output the results
int count = 0;
for (int i = 2; i < primes.Length; i++) {
if (primes[i - 1]) {
Console.WriteLine(i);
count++;
}
}
Console.WriteLine($"There are {count} primes up to {MAX}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment