mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-07 10:43:58 -05:00
Added function for SieveOfEratosthenes
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//C#/CSClasses/Algorithms.cs
|
//C#/CSClasses/Algorithms.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 08-23-20
|
// 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
|
//This file contains a class that is used to time the execution time of other programs
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -836,5 +836,70 @@ namespace mee{
|
|||||||
}
|
}
|
||||||
return binaryString;
|
return binaryString;
|
||||||
}
|
}
|
||||||
|
//Sieve of Eratosthenese for generating primes
|
||||||
|
public static IEnumerable<long> SieveOfEratosthenes(){
|
||||||
|
yield return 2;
|
||||||
|
|
||||||
|
//A dictionary for holding the results of all primes that have already been found
|
||||||
|
Dictionary<long, List<long>> dict = new Dictionary<long, List<long>>();
|
||||||
|
|
||||||
|
//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<long> tempList = new List<long>(){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<long> tempList = new List<long>(){num};
|
||||||
|
dict.Add(num + num + possiblePrime, tempList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//We no longer need this, free the memory
|
||||||
|
dict.Remove(possiblePrime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static IEnumerable<BigInteger> SieveOfEratosthenesBig(){
|
||||||
|
yield return 2;
|
||||||
|
|
||||||
|
//A dictionary for holding the results of all primes that have already been found
|
||||||
|
Dictionary<BigInteger, List<BigInteger>> dict = new Dictionary<BigInteger, List<BigInteger>>();
|
||||||
|
|
||||||
|
//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<BigInteger> tempList = new List<BigInteger>(){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<BigInteger> tempList = new List<BigInteger>(){num};
|
||||||
|
dict.Add(num + num + possiblePrime, tempList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//We no longer need this, free the memory
|
||||||
|
dict.Remove(possiblePrime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
|
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-11-21
|
// Created: 03-11-21
|
||||||
//Modified: 06-29-21
|
//Modified: 06-30-21
|
||||||
//This file contains the tests for the Algorithms class
|
//This file contains the tests for the Algorithms class
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -400,5 +400,27 @@ namespace TestCSClasses{
|
|||||||
answer = mee.Algorithms.ToBin(bigNum);
|
answer = mee.Algorithms.ToBin(bigNum);
|
||||||
Assert.AreEqual(correctAnswer, answer, "ToBin big 3 failed");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user