Files
CSClasses/CSClasses/Algorithms.cs

612 lines
20 KiB
C#

//C#/CSClasses/Algorithms.cs
//Matthew Ellison
// Created: 08-23-20
//Modified: 08-23-20
//This file contains a class that is used to time the execution time of other programs
/*
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace mee{
public class Algorithms{
//These functions return a list of all Fibonacci numbers <= goalNumber
public static List<int> GetAllFib(int goalNumber){
//Setup the variables
List<int> fibNums = new List<int>();
//If the number is <= 0 return an empty list
if(goalNumber <= 0){
return fibNums;
}
//This means that at least 2 1's are elements
fibNums.Add(1);
fibNums.Add(1);
//Loop to generate the rest of the Fibonacci numbers
while(fibNums[fibNums.Count - 1] <= goalNumber){
fibNums.Add(fibNums[fibNums.Count - 1] + fibNums[fibNums.Count - 2]);
}
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
fibNums.RemoveAt(fibNums.Count - 1);
return fibNums;
}
public static List<long> GetAllFib(long goalNumber){
//Setup the variables
List<long> fibNums = new List<long>();
//If the number is <= 0 return an empty list
if(goalNumber <= 0){
return fibNums;
}
//This means that at least 2 1's are elements
fibNums.Add(1);
fibNums.Add(1);
//Loop to generate the rest of the Fibonacci numbers
while(fibNums[fibNums.Count - 1] <= goalNumber){
fibNums.Add(fibNums[fibNums.Count - 1] + fibNums[fibNums.Count - 2]);
}
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
fibNums.RemoveAt(fibNums.Count - 1);
return fibNums;
}
public static List<BigInteger> GetAllFib(BigInteger goalNumber){
//Setup the variables
List<BigInteger> fibNums = new List<BigInteger>();
//If the number is <= 0 return am empty list
if(goalNumber <= 0){
return fibNums;
}
//This means that at least 2 1's are elements
fibNums.Add(1);
fibNums.Add(1);
//Loop to generate the rest of Fibonacci numbers
while(fibNums[fibNums.Count - 1] <= goalNumber){
fibNums.Add(fibNums[fibNums.Count - 1] + fibNums[fibNums.Count - 2]);
}
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
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;
}
//If for some reason the goalNumber is not 1 throw an exception
if(goalNumber != 1){
throw new mee.Exceptions.InvalidResult("Factor did not end as 1");
}
//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;
}
//If for some reason the goalNumber is not 1 throw an exception
if(goalNumber != 1){
throw new mee.Exceptions.InvalidResult("Factor did not end as 1");
}
//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;
}
//If for some reason the goalNumber is not 1 throw an exception
if(goalNumber != 1){
throw new mee.Exceptions.InvalidResult("Factor did not end as 1");
}
//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;
}
//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;
}
//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();
}
public static long GetSum(List<long> ary){
return ary.Sum();
}
public static BigInteger GetSum(List<BigInteger> ary){
BigInteger sum = 0;
foreach(BigInteger num in ary){
sum += num;
}
return sum;
}
public static int GetProd(List<int> ary){
int prod = 1;
foreach(int num in ary){
prod *= num;
}
return prod;
}
public static long GetProd(List<long> ary){
long prod = 1;
foreach(long num in ary){
prod *= num;
}
return prod;
}
public static BigInteger GetProd(List<BigInteger> ary){
BigInteger prod = 1;
foreach(BigInteger num in ary){
prod *= num;
}
return prod;
}
}
}