Added GetDivisors function

This commit is contained in:
2020-08-24 13:53:38 -04:00
parent b2cab43514
commit 6725c5fe29

View File

@@ -460,6 +460,109 @@ namespace mee{
primes.Sort();
return primes;
}
//These functions return all the divisors of goalNumber
public static List<int> GetDivisors(int goalNumber){
List<int> divisors = new List<int>();
//Start by checking that ht enumber is positive
if(goalNumber <= 0){
return divisors;
}
//If the number is 1 return just itself
else if(goalNumber == 1){
divisors.Add(1);
return divisors;
}
//Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly
double topPossibleDivisor = Math.Ceiling(Math.Sqrt(goalNumber));
for(int possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
//If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0){
divisors.Add(possibleDivisor);
//Account for the pssibility of sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor){
divisors.Add(goalNumber / possibleDivisor);
}
//Take care of a few occations where a number was added twice
if(divisors[divisors.Count - 1] == (possibleDivisor + 1)){
++possibleDivisor;
}
}
}
//Sort the list before returning it for neatness
divisors.Sort();
//Return the list
return divisors;
}
public static List<long> GetDivisors(long goalNumber){
List<long> divisors = new List<long>();
//Start by checking that ht enumber is positive
if(goalNumber <= 0){
return divisors;
}
//If the number is 1 return just itself
else if(goalNumber == 1){
divisors.Add(1);
return divisors;
}
//Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly
double topPossibleDivisor = Math.Ceiling(Math.Sqrt(goalNumber));
for(long possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
//If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0){
divisors.Add(possibleDivisor);
//Account for the pssibility of sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor){
divisors.Add(goalNumber / possibleDivisor);
}
//Take care of a few occations where a number was added twice
if(divisors[divisors.Count - 1] == (possibleDivisor + 1)){
++possibleDivisor;
}
}
}
//Sort the list before returning it for neatness
divisors.Sort();
//Return the list
return divisors;
}
public static List<BigInteger> GetDivisors(BigInteger goalNumber){
List<BigInteger> divisors = new List<BigInteger>();
//Start by checking that ht enumber is positive
if(goalNumber <= 0){
return divisors;
}
//If the number is 1 return just itself
else if(goalNumber == 1){
divisors.Add(1);
return divisors;
}
//Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly
double topPossibleDivisor = Math.Exp(BigInteger.Log(goalNumber) / 2);
for(long possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
//If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0){
divisors.Add(possibleDivisor);
//Account for the pssibility of sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor){
divisors.Add(goalNumber / possibleDivisor);
}
//Take care of a few occations where a number was added twice
if(divisors[divisors.Count - 1] == (possibleDivisor + 1)){
++possibleDivisor;
}
}
}
//Sort the list before returning it for neatness
divisors.Sort();
//Return the list
return divisors;
}
//These functions get a value from combining elements in an array
public static int GetSum(List<int> ary){
return ary.Sum();