Skip to content

Commit 7843dd8

Browse files
authored
Merge pull request aalhour#73 from adamfisher/Implement_Sieve_Of_Atkins
Added Sieve of Atkin algorithm for finding prime numbers.
2 parents 439c9cf + 0e4b4a8 commit 7843dd8

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Algorithms/Numeric/SieveOfAtkin.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using System.Reflection.Metadata.Ecma335;
3+
using System.Threading.Tasks;
4+
5+
/***
6+
* Generates all prime numbers up to a given number
7+
* Wikipedia: https://en.wikipedia.org/wiki/Sieve_of_Atkin
8+
*/
9+
10+
11+
namespace Algorithms.Numeric
12+
{
13+
public static class SieveOfAtkin
14+
{
15+
/// <summary>
16+
/// Calculate primes up to a given number
17+
/// </summary>
18+
public static List<int> GeneratePrimesUpTo(int max)
19+
{
20+
if (max == 2)
21+
{
22+
return new List<int>() { 2 };
23+
}
24+
25+
if (max < 2)
26+
{
27+
return new List<int>();
28+
}
29+
30+
var isPrime = new bool[max + 1];
31+
var sqrt = (int)System.Math.Sqrt(max);
32+
33+
Parallel.For(1, sqrt, x =>
34+
{
35+
var xx = x * x;
36+
for (var y = 1; y <= sqrt; y++)
37+
{
38+
var yy = y * y;
39+
var n = 4 * xx + yy;
40+
if (n <= max && (n % 12 == 1 || n % 12 == 5))
41+
isPrime[n] ^= true;
42+
43+
n = 3 * xx + yy;
44+
if (n <= max && n % 12 == 7)
45+
isPrime[n] ^= true;
46+
47+
n = 3 * xx - yy;
48+
if (x > y && n <= max && n % 12 == 11)
49+
isPrime[n] ^= true;
50+
}
51+
});
52+
53+
var primes = new List<int>() { 2, 3 };
54+
for (var n = 5; n <= sqrt; n++)
55+
{
56+
if (isPrime[n])
57+
{
58+
primes.Add(n);
59+
var nn = n * n;
60+
for (var k = nn; k <= max; k += nn)
61+
isPrime[k] = false;
62+
}
63+
}
64+
65+
for (var n = sqrt + 1; n <= max; n++)
66+
if (isPrime[n])
67+
primes.Add(n);
68+
69+
return primes;
70+
}
71+
72+
}
73+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Linq;
2+
using Algorithms.Numeric;
3+
using Xunit;
4+
5+
namespace UnitTest.AlgorithmsTests
6+
{
7+
public class SieveOfAtkinTest
8+
{
9+
[Theory]
10+
[InlineData("Primes up to -100", -100)]
11+
[InlineData("Primes up to 2", 2, 2)]
12+
[InlineData("Primes up to 3", 3, 2, 3)]
13+
[InlineData("Primes up to 100", 100, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)]
14+
public static void GeneratePrimesUpTo(string test, int max, params int[] expectedResult)
15+
{
16+
var result = SieveOfAtkin.GeneratePrimesUpTo(max);
17+
Assert.True(result.SequenceEqual(expectedResult), test);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)