464 lines
15 KiB
C#
464 lines
15 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.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;
|
|
}
|
|
|
|
//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;
|
|
}
|
|
//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;
|
|
}
|
|
}
|
|
}
|