-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
90 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,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; | ||
} | ||
|
||
} | ||
} |
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,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)); | ||
|
||
} | ||
} | ||
} |