Added getNumPrimes function

This commit is contained in:
2020-08-23 22:15:20 -04:00
parent b52c6bc23d
commit 72dfbd67d1

View File

@@ -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<int> getNumPrimes(int numberOfPrimes){
List<int> primes = new List<int>(); //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<long> getNumPrimes(long numberOfPrimes){
List<long> primes = new List<long>(); //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<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
List<BigInteger> primes = new List<BigInteger>(); //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;