public void PentaTest() { long lPenta; List <long> llPentas = new List <long>(2000); Assert.AreEqual(1, PolygonalSieve.GetPentagonal(1)); Assert.AreEqual(5, PolygonalSieve.GetPentagonal(2)); Assert.AreEqual(12, PolygonalSieve.GetPentagonal(3)); Assert.AreEqual(22, PolygonalSieve.GetPentagonal(4)); Assert.AreEqual(35, PolygonalSieve.GetPentagonal(5)); Assert.AreEqual(51, PolygonalSieve.GetPentagonal(6)); Assert.AreEqual(70, PolygonalSieve.GetPentagonal(7)); for (int i = 1; i < 2000; i++) { lPenta = PolygonalSieve.GetPentagonal(i); Assert.AreEqual(i, PolygonalSieve.GetPentagonalIndex(lPenta)); llPentas.Add(lPenta); } for (int i = 1; i < 2000; i++) { Assert.AreEqual(llPentas.Contains(i), PolygonalSieve.IsPentagonal(i)); } }
public void PentaSpeedTest() { //Used to determine fastest testing algorithm //Winner was HashSet, but only by 2x. //BinarySearch broke for larger Lists /* Sample Output: * * Gen List: 00:00.00s * Gen Hash: 00:00.00s * Correctness: 00:00.45s * Static Numbers: 00:00.01s //No memory needs, no pre-computed table needed * List Contains: 00:04.61s //REALLY TERRIBLE * List BinarySearch: 00:00.01s //Breaks on List.Count > 300000 * Hash Contains: 00:00.00s //Fastest runtime, large memory requirements * * Gen List: 00:00.00s * Gen Hash: 00:00.00s * Correctness: 00:00.46s * Static Numbers: 00:18.11s * List BinarySearch: 00:36.13s * Hash Contains: 00:09.43s */ HashSet <long> hl = new HashSet <long>(); List <long> ll = new List <long>(); long lPenta; bool b; long lTestTo = 300; Ticker t = new Ticker(); for (int i = 1; i < 30000; i++) { lPenta = PolygonalSieve.GetPentagonal(i); ll.Add(lPenta); } t.Tick("Gen List"); for (int i = 1; i < 30000; i++) { lPenta = PolygonalSieve.GetPentagonal(i); hl.Add(lPenta); } t.Tick("Gen Hash"); //Test Correctness for (long i = 1; i < 3000; i++) { b = PolygonalSieve.IsPentagonal(i); //Assert.AreEqual(b, ll.Contains(i)); Assert.AreEqual(b, ll.BinarySearch(i) >= 0); Assert.AreEqual(b, hl.Contains(i)); } t.Tick("Correctness"); for (long i = 1; i < lTestTo; i++) { b = PolygonalSieve.IsPentagonal(i); } t.Tick("Static Numbers"); //Clear loser, all other tests completely eclipse this one, commented out so that the test can give better numbers //for (int i = 1; i < iTestTo; i++) { // b = ll.Contains(i); //} //t.Tick("List Contains"); for (long i = 1; i < lTestTo; i++) { b = ll.BinarySearch(i) >= 0; } t.Tick("List BinarySearch"); for (long i = 1; i < lTestTo; i++) { b = hl.Contains(i); } t.Tick("Hash Contains"); }