diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 982c654..1d05be1 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -318,6 +318,143 @@ namespace mee{ foundFactor = false; } } + + //Sort the list before returning it + primes.Sort(); + return primes; + } + //This function gets a certain number of primes + public static List getNumPrimes(int numberOfPrimes){ + List primes = new List(); //Holds the prime numbers + bool foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the number is 0 or negative return an empty list + if(numberOfPrimes <= 1){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.Add(2); + } + + //We can now start at 3 and skip all even numbers, because they cannot be prime + for(int possiblePrime = 3;primes.Count < numberOfPrimes;possiblePrime += 2){ + //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor + int topPossibleFactor = (int)Math.Ceiling(Math.Sqrt(possiblePrime)); + //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this + for(int primesCnt = 0;primes[primesCnt] <= topPossibleFactor;){ + if((possiblePrime % primes[primesCnt]) == 0){ + foundFactor = true; + break; + } + else{ + ++primesCnt; + } + //Check if the index has gone out of bounds + if(primesCnt >= primes.Count){ + break; + } + } + + //If you didn't find a factor then the current number must be prime + if(!foundFactor){ + primes.Add(possiblePrime); + } + else{ + foundFactor = false; + } + } + + //Sort the list before returning it + primes.Sort(); + return primes; + } + public static List getNumPrimes(long numberOfPrimes){ + List primes = new List(); //Holds the prime numbers + bool foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the number is 0 or negative return an empty list + if(numberOfPrimes <= 1){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.Add(2); + } + + //We can now start at 3 and skip all even numbers, because they cannot be prime + for(long possiblePrime = 3;primes.Count < numberOfPrimes;possiblePrime += 2){ + //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor + long topPossibleFactor = (long)Math.Ceiling(Math.Sqrt(possiblePrime)); + //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this + for(int primesCnt = 0;primes[primesCnt] <= topPossibleFactor;){ + if((possiblePrime % primes[primesCnt]) == 0){ + foundFactor = true; + break; + } + else{ + ++primesCnt; + } + //Check if the index has gone out of bounds + if(primesCnt >= primes.Count){ + break; + } + } + + //If you didn't find a factor then the current number must be prime + if(!foundFactor){ + primes.Add(possiblePrime); + } + else{ + foundFactor = false; + } + } + + //Sort the list before returning it + primes.Sort(); + return primes; + } + public static List getNumPrimes(BigInteger numberOfPrimes){ + List primes = new List(); //Holds the prime numbers + bool foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the number is 0 or negative return an empty list + if(numberOfPrimes <= 1){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.Add(2); + } + + //We can now start at 3 and skip all even numbers, because they cannot be prime + for(BigInteger possiblePrime = 3;primes.Count < numberOfPrimes;possiblePrime += 2){ + //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor + BigInteger topPossibleFactor = (BigInteger)Math.Exp(BigInteger.Log(possiblePrime) / 2); + //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this + for(int primesCnt = 0;primes[primesCnt] <= topPossibleFactor;){ + if((possiblePrime % primes[primesCnt]) == 0){ + foundFactor = true; + break; + } + else{ + ++primesCnt; + } + //Check if the index has gone out of bounds + if(primesCnt >= primes.Count){ + break; + } + } + + //If you didn't find a factor then the current number must be prime + if(!foundFactor){ + primes.Add(possiblePrime); + } + else{ + foundFactor = false; + } + } + //Sort the list before returning it primes.Sort(); return primes;