Added function for SieveOfEratosthenes

This commit is contained in:
2021-06-30 12:02:13 -04:00
parent 5ac02757f4
commit 34edb993f2
2 changed files with 89 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
//Matthew Ellison
// Created: 03-11-21
//Modified: 06-29-21
//Modified: 06-30-21
//This file contains the tests for the Algorithms class
/*
Copyright (C) 2021 Matthew Ellison
@@ -400,5 +400,27 @@ namespace TestCSClasses{
answer = mee.Algorithms.ToBin(bigNum);
Assert.AreEqual(correctAnswer, answer, "ToBin big 3 failed");
}
[TestMethod]
public void TestSieveOfEratosthenes(){
//Test 1
IEnumerator<long> sieve = mee.Algorithms.SieveOfEratosthenes().GetEnumerator();
List<long> correctAnswer = new List<long>(){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};
List<long> answer = new List<long>();
for(int cnt = 0;cnt < 25;++cnt){
sieve.MoveNext();
answer.Add(sieve.Current);
}
CollectionAssert.AreEqual(correctAnswer, answer, "SieveOfEratosthenes failed");
//Test 2
IEnumerator<BigInteger> bigSieve = mee.Algorithms.SieveOfEratosthenesBig().GetEnumerator();
List<BigInteger> bigCorrectAnswer = new List<BigInteger>(){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};
List<BigInteger> bigAnswer = new List<BigInteger>();
for(int cnt = 0;cnt < 25;++cnt){
bigSieve.MoveNext();
bigAnswer.Add(bigSieve.Current);
}
CollectionAssert.AreEqual(bigCorrectAnswer, bigAnswer, "SieveOfEratosthenesBig failed");
}
}
}