From 34edb993f2c1457be4d8e2eec64ab2bf3c7a9f08 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 30 Jun 2021 12:02:13 -0400 Subject: [PATCH] Added function for SieveOfEratosthenes --- CSClasses/Algorithms.cs | 67 ++++++++++++++++++++++++++++++++- TestCSClasses/TestAlgorithms.cs | 24 +++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 03ca134..067fbcc 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -1,7 +1,7 @@ //C#/CSClasses/Algorithms.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 06-29-21 +//Modified: 06-30-21 //This file contains a class that is used to time the execution time of other programs /* Copyright (C) 2021 Matthew Ellison @@ -836,5 +836,70 @@ namespace mee{ } return binaryString; } + //Sieve of Eratosthenese for generating primes + public static IEnumerable SieveOfEratosthenes(){ + yield return 2; + + //A dictionary for holding the results of all primes that have already been found + Dictionary> dict = new Dictionary>(); + + //Start checking for primes with the number 3 and skip all even numbers + for(long possiblePrime = 3;true;possiblePrime += 2){ + //If possiblePrime is not in the dictionary it is a new prime number + //Return it and mark its next multiple + if(!dict.ContainsKey(possiblePrime)){ + yield return possiblePrime; + List tempList = new List(){possiblePrime}; + dict.Add(possiblePrime * possiblePrime, tempList); + } + //If possiblePrime is in the dictionary it is a composite number + else{ + //Move each witness to it's next multiple + foreach(long num in dict[possiblePrime]){ + if(dict.ContainsKey(num + num + possiblePrime)){ + dict[num + num + possiblePrime].Add(num); + } + else{ + List tempList = new List(){num}; + dict.Add(num + num + possiblePrime, tempList); + } + } + //We no longer need this, free the memory + dict.Remove(possiblePrime); + } + } + } + public static IEnumerable SieveOfEratosthenesBig(){ + yield return 2; + + //A dictionary for holding the results of all primes that have already been found + Dictionary> dict = new Dictionary>(); + + //Start checking for priems with the number 3 and skip all even numbers + for(BigInteger possiblePrime = 3;true;possiblePrime += 2){ + //If possiblePrime is not in the dictionary it is a new priem number + //Return it and mark its next multiple + if(!dict.ContainsKey(possiblePrime)){ + yield return possiblePrime; + List tempList = new List(){possiblePrime}; + dict.Add(possiblePrime * possiblePrime, tempList); + } + //If possiblePrime is in the dictionary it is a composite number + else{ + //Move each witness to ti's next multiple + foreach(BigInteger num in dict[possiblePrime]){ + if(dict.ContainsKey(num + num + possiblePrime)){ + dict[num + num + possiblePrime].Add(num); + } + else{ + List tempList = new List(){num}; + dict.Add(num + num + possiblePrime, tempList); + } + } + //We no longer need this, free the memory + dict.Remove(possiblePrime); + } + } + } } } diff --git a/TestCSClasses/TestAlgorithms.cs b/TestCSClasses/TestAlgorithms.cs index 4174027..eaa83b7 100644 --- a/TestCSClasses/TestAlgorithms.cs +++ b/TestCSClasses/TestAlgorithms.cs @@ -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 sieve = mee.Algorithms.SieveOfEratosthenes().GetEnumerator(); + List correctAnswer = new List(){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 answer = new List(); + for(int cnt = 0;cnt < 25;++cnt){ + sieve.MoveNext(); + answer.Add(sieve.Current); + } + CollectionAssert.AreEqual(correctAnswer, answer, "SieveOfEratosthenes failed"); + + //Test 2 + IEnumerator bigSieve = mee.Algorithms.SieveOfEratosthenesBig().GetEnumerator(); + List bigCorrectAnswer = new List(){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 bigAnswer = new List(); + for(int cnt = 0;cnt < 25;++cnt){ + bigSieve.MoveNext(); + bigAnswer.Add(bigSieve.Current); + } + CollectionAssert.AreEqual(bigCorrectAnswer, bigAnswer, "SieveOfEratosthenesBig failed"); + } } }