mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Added getFactors and getPrimes functions
This commit is contained in:
@@ -95,5 +95,232 @@ namespace mee{
|
||||
fibNums.RemoveAt(fibNums.Count - 1);
|
||||
return fibNums;
|
||||
}
|
||||
//These functions return all factors of goalNumber
|
||||
public static List<int> getFactors(int goalNumber){
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
int topPossiblePrime = (int)Math.Ceiling(Math.Sqrt(goalNumber));
|
||||
List<int> primes = getPrimes(topPossiblePrime);
|
||||
List<int> factors = new List<int>();
|
||||
|
||||
//YOu need to step through each prime and see if it is a factor in the number
|
||||
for(int cnt = 0;cnt < primes.Count;){
|
||||
//If the prime is a factor you need to add it to the factor list
|
||||
if((goalNumber % primes[cnt]) == 0){
|
||||
factors.Add(primes[cnt]);
|
||||
goalNumber /= primes[cnt];
|
||||
}
|
||||
//Otherwise advance the location in primes you are looking at
|
||||
//By noit advancing if the prime is a factor you allow for multiple of the same prime as a factor
|
||||
else{
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't get any factors the number itself must be a prime
|
||||
if(factors.Count == 0){
|
||||
factors.Add(goalNumber);
|
||||
goalNumber /= goalNumber;
|
||||
}
|
||||
|
||||
//TODO: If for some reason the goalNumber is not 1 throw an error
|
||||
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
public static List<long> getFactors(long goalNumber){
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
long topPossiblePrime = (long)Math.Ceiling(Math.Sqrt(goalNumber));
|
||||
List<long> primes = getPrimes(topPossiblePrime);
|
||||
List<long> factors = new List<long>();
|
||||
|
||||
//YOu need to step through each prime and see if it is a factor in the number
|
||||
for(int cnt = 0;cnt < primes.Count;){
|
||||
//If the prime is a factor you need to add it to the factor list
|
||||
if((goalNumber % primes[cnt]) == 0){
|
||||
factors.Add(primes[cnt]);
|
||||
goalNumber /= primes[cnt];
|
||||
}
|
||||
//Otherwise advance the location in primes you are looking at
|
||||
//By noit advancing if the prime is a factor you allow for multiple of the same prime as a factor
|
||||
else{
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't get any factors the number itself must be a prime
|
||||
if(factors.Count == 0){
|
||||
factors.Add(goalNumber);
|
||||
goalNumber /= goalNumber;
|
||||
}
|
||||
|
||||
//TODO: If for some reason the goalNumber is not 1 throw an error
|
||||
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
public static List<BigInteger> getFactors(BigInteger goalNumber){
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
BigInteger topPossiblePrime = (BigInteger)Math.Exp(BigInteger.Log(goalNumber) / 2);
|
||||
List<BigInteger> primes = getPrimes(topPossiblePrime);
|
||||
List<BigInteger> factors = new List<BigInteger>();
|
||||
|
||||
//YOu need to step through each prime and see if it is a factor in the number
|
||||
for(int cnt = 0;cnt < primes.Count;){
|
||||
//If the prime is a factor you need to add it to the factor list
|
||||
if((goalNumber % primes[cnt]) == 0){
|
||||
factors.Add(primes[cnt]);
|
||||
goalNumber /= primes[cnt];
|
||||
}
|
||||
//Otherwise advance the location in primes you are looking at
|
||||
//By noit advancing if the prime is a factor you allow for multiple of the same prime as a factor
|
||||
else{
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't get any factors the number itself must be a prime
|
||||
if(factors.Count == 0){
|
||||
factors.Add(goalNumber);
|
||||
goalNumber /= goalNumber;
|
||||
}
|
||||
|
||||
//TODO: If for some reason the goalNumber is not 1 throw an error
|
||||
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
//These functions return a list with all the prime number <= goalNumber
|
||||
public static List<int> getPrimes(int goalNumber){
|
||||
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(goalNumber <= 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;possiblePrime <= goalNumber;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 range
|
||||
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> getPrimes(long goalNumber){
|
||||
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(goalNumber <= 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;possiblePrime <= goalNumber;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 range
|
||||
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> getPrimes(BigInteger goalNumber){
|
||||
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(goalNumber <= 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;possiblePrime <= goalNumber;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 range
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user