Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
A De Bok committed Jan 23, 2020
0 parents commit 16fa264
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
obj/
bin/
84 changes: 84 additions & 0 deletions Eratosthenes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace EratosthenesPrimes
{
static class Eratosthenes
{
public static List<long> FindPrimes(long maxNum, int sampleSize = 1000)
{
if (maxNum < 2)
{
return new List<long>();
}
List<long> foundPrimes = new List<long> { 2 };
long sampleStart = 3;
long largestSample = 2;

while (largestSample < maxNum)
{
var realSampleSize = maxNum - largestSample < sampleSize ? (int)(maxNum - largestSample) : sampleSize;
var sample = Enumerable.Repeat(true, realSampleSize).ToArray();

largestSample = sampleStart + realSampleSize;
var sqrt = Math.Sqrt(largestSample);

foreach (var prime in foundPrimes)
{
if (prime > sqrt)
{
// None of the composite numbers in our current sample will be divisible by a prime > sqrt.
break;
}

var multiple = prime * (sampleStart / prime); // The largest multiple less than or equal to sampleStart.
multiple += multiple < sampleStart ? prime : 0;
while (multiple < largestSample)
{
sample[multiple - sampleStart] = false;
multiple += prime;
}
}
for (int i = 0; i < sample.Length; i++)
{
if (sample[i])
{
var prime = sampleStart + i;
foundPrimes.Add(prime);

var multiple = prime * 2;
while (multiple < largestSample)
{
sample[multiple - sampleStart] = false;
multiple += prime;
}
}
}

sampleStart = largestSample;
}

return foundPrimes;
}

public static bool IsPrime(long prime)
{
if(prime < 2)
{
return false;
}

long sqrt = (long)Math.Sqrt(prime);
foreach(var i in FindPrimes(sqrt))
{
if(prime % i == 0)
{
return false;
}
}

return true;
}
}
}
8 changes: 8 additions & 0 deletions EratosthenesPrimes.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

</Project>
43 changes: 43 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Diagnostics;

namespace EratosthenesPrimes
{
class Program
{
static void Main(string[] args)
{
long limit = 1000;
int size = 1000;

if(args.Length >= 1)
{
if(!Int64.TryParse(args[0], out limit))
{
limit = 1000;
}
}
if(args.Length >= 2)
{
if(!Int32.TryParse(args[1], out size))
{
size = 1000;
}
}

var sw = new Stopwatch();
sw.Start();
var primes = Eratosthenes.FindPrimes(limit, size);
sw.Stop();
Console.WriteLine($"{primes.Count} primes found in {sw.Elapsed.Seconds}.{sw.ElapsedMilliseconds % 1000, 3:D3} seconds");

Console.WriteLine("Press Enter to print the primes or Ctrl-C to quit.");
Console.ReadLine();

foreach(var p in primes)
{
Console.WriteLine(p);
}
}
}
}

0 comments on commit 16fa264

Please sign in to comment.