diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 18ed00a..4a50d57 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -460,6 +460,109 @@ namespace mee{ primes.Sort(); return primes; } + //These functions return all the divisors of goalNumber + public static List GetDivisors(int goalNumber){ + List divisors = new List(); + //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 GetDivisors(long goalNumber){ + List divisors = new List(); + //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 GetDivisors(BigInteger goalNumber){ + List divisors = new List(); + //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 ary){ return ary.Sum();