Skip to content

Commit 0bb048a

Browse files
author
Matthew Detweiler
committed
Updated Sieve of Eratosthenes to utilize Hashset instead of list internally. Now returns an IEnumerable instead of list. Implemented unit tests
1 parent f646ffd commit 0bb048a

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

Algorithms/Numeric/SieveOfEratosthenes.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
/***
56
* Generates all prime numbers up to a given number
@@ -14,43 +15,43 @@ public static class SieveOfEratosthenes
1415
/// <summary>
1516
/// Calculate primes up to a given number
1617
/// </summary>
17-
public static List<int> GeneratePrimesUpTo(int x)
18+
public static IEnumerable<int> GeneratePrimesUpTo(int x)
1819
{
1920

20-
//The list of primes that will be returned
21-
List<int> primesList = new List<int>();
21+
//The hash of primes that will be returned
22+
var primes = new HashSet<int>();
2223

2324
//Returns an empty list if x is a value under 2
2425
if (x < 2)
2526
{
26-
return primesList;
27+
return primes.ToList();
2728
}
2829

29-
//Adds every number between 2 and x to the list
30+
//Adds every number between 2 and x to the hashset
3031
for (int i = 2; i <= x; i++)
3132
{
32-
primesList.Add(i);
33+
primes.Add(i);
3334
}
3435

35-
//integer that all multiples of will be removed from the list
36+
//integer that all multiples of will be removed from the hashset
3637
int removeMultiplesOf;
3738

38-
//Finds the next number in the list that hasn't been removed and removes all multiples of that number
39-
//from the list
39+
//Finds the next number that hasn't been removed and removes all multiples of that number
40+
//from the hashset
4041
for (int i = 2; i <= Math.Sqrt(x); i++)
4142
{
42-
if (primesList.Contains(i))
43+
if (primes.Contains(i))
4344
{
4445
removeMultiplesOf = i;
45-
for (int j = removeMultiplesOf*removeMultiplesOf; j <= x; j += removeMultiplesOf)
46+
for (int j = removeMultiplesOf * removeMultiplesOf; j <= x; j += removeMultiplesOf)
4647
{
47-
primesList.Remove(j);
48+
primes.Remove(j);
4849
}
4950
}
5051
}
5152

5253
//The list of primes is returned
53-
return primesList;
54+
return primes.ToList();
5455
}
5556

5657
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Algorithms.Numeric;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace UnitTest.AlgorithmsTests
6+
{
7+
public class SieveOfEratosthenesTests
8+
{
9+
private const int MaxNumber = 100;
10+
11+
[Fact]
12+
public void SieveOfEratosthenesGeneratesCorrectResults()
13+
{
14+
var results = SieveOfEratosthenes.GeneratePrimesUpTo(MaxNumber);
15+
Assert.NotNull(results);
16+
Assert.True(results.Any());
17+
Assert.Equal(results.Count(), 25);
18+
Assert.DoesNotContain(1, results);
19+
Assert.Contains(2, results);
20+
Assert.Contains(7, results);
21+
Assert.Contains(23, results);
22+
Assert.Contains(41, results);
23+
Assert.Contains(97, results);
24+
25+
}
26+
27+
[Fact]
28+
public void SieveOfEratosthenesReturnsEmptyListWhenGiven0()
29+
{
30+
var results = SieveOfEratosthenes.GeneratePrimesUpTo(0);
31+
Assert.NotNull(results);
32+
Assert.False(results.Any());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)