Skip to content

Commit

Permalink
Sieve of Eratosthenes (#49)
Browse files Browse the repository at this point in the history
* Sieve Of Eratosthenes

An algorithm to generate all prime numbers up to a certain number

* Sieve of Eratosthenes test

Tests the Sieve of Eratosthenese implementation

* Delete SieveOfEratosthenes.cs

* Add files via upload

* Delete SieveOfEratosthenesTest.cs

* Add files via upload

* Added Sieve of Eratosthenes test

Added Sieve of Eratosthenes test
  • Loading branch information
maxm2017 authored and aalhour committed Nov 19, 2017
1 parent 6b2bd05 commit a9d335e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Algorithms/Numeric/SieveOfEratosthenes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;

/***
* Generates all prime numbers up to a given number
* Wikipedia: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
*/


namespace Algorithms.Numeric
{
public static class SieveOfEratosthenes
{
/// <summary>
/// Calculate primes up to a given number
/// </summary>
public static List<int> GeneratePrimesUpTo(int x)
{

//The list of primes that will be returned
List<int> primesList = new List<int>();

//Returns an empty list if x is a value under 2
if (x < 2)
{
return primesList;
}

//Adds every number between 2 and x to the list
for (int i = 2; i <= x; i++)
{
primesList.Add(i);
}

//integer that all multiples of will be removed from the list
int removeMultiplesOf;

//Finds the next number in the list that hasn't been removed and removes all multiples of that number
//from the list
for (int i = 2; i <= Math.Sqrt(x); i++)
{
if (primesList.Contains(i))
{
removeMultiplesOf = i;
for (int j = removeMultiplesOf*removeMultiplesOf; j <= x; j += removeMultiplesOf)
{
primesList.Remove(j);
}
}
}

//The list of primes is returned
return primesList;
}

}
}
33 changes: 33 additions & 0 deletions MainProgram/AlgorithmsTests/SieveOfEratosthenesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace C_Sharp_Algorithms.AlgorithmsTests
{
class SieveOfEratosthenesTest
{

//Test lists
static List<int> testList = new List<int>();
static List<int> primesUpTo100List = new List<int>{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};
static List<int> primesUpTo2List = new List<int> { 2 };
static List<int> emptyList = new List<int>();

public static void DoTest()
{
//Check if algorithm returns expected lists
testList = Algorithms.Numeric.SieveOfEratosthenes.GeneratePrimesUpTo(100);
Debug.Assert(testList.SequenceEqual(primesUpTo100List));

testList = Algorithms.Numeric.SieveOfEratosthenes.GeneratePrimesUpTo(2);
Debug.Assert(testList.SequenceEqual(primesUpTo2List));

testList = Algorithms.Numeric.SieveOfEratosthenes.GeneratePrimesUpTo(1);
Debug.Assert(testList.SequenceEqual(emptyList));

testList = Algorithms.Numeric.SieveOfEratosthenes.GeneratePrimesUpTo(-100);
Debug.Assert(testList.SequenceEqual(emptyList));

}
}
}

0 comments on commit a9d335e

Please sign in to comment.