-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
A De Bok
committed
Jan 23, 2020
0 parents
commit 16fa264
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
obj/ | ||
bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |