diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 0e1e82b..982c654 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -95,5 +95,232 @@ namespace mee{ fibNums.RemoveAt(fibNums.Count - 1); return fibNums; } + //These functions return all factors of goalNumber + public static List 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 primes = getPrimes(topPossiblePrime); + List factors = new List(); + + //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 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 primes = getPrimes(topPossiblePrime); + List factors = new List(); + + //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 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 primes = getPrimes(topPossiblePrime); + List factors = new List(); + + //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 getPrimes(int goalNumber){ + 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(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 getPrimes(long goalNumber){ + 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(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 getPrimes(BigInteger goalNumber){ + 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(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; + } } }