Various bug fixes

This commit is contained in:
2021-03-11 14:53:45 -05:00
parent fa5b4d2f15
commit 4ac0db8d6d

View File

@@ -32,7 +32,18 @@ namespace mee{
//These are a few functions that help with some utility within the class
//Returns the sqrt of the BigInt passed to it
public static BigInteger BigIntegerSqrt(BigInteger num){
return (BigInteger)Math.Ceiling(Math.Exp(BigInteger.Log(num) / 2));
BigInteger sqrt;
//Get the sqrt from the general formula
double tempSqrt = Math.Exp(BigInteger.Log(num) / 2);
//Account for floating-point rounding errors
if((BigInteger)(tempSqrt * tempSqrt) < num){
sqrt = (BigInteger)Math.Ceiling(tempSqrt);
}
else{
sqrt = (BigInteger)tempSqrt;
}
return sqrt;
}
//These functions return a list of all Fibonacci numbers <= goalNumber
@@ -557,7 +568,7 @@ namespace mee{
}
//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);
double topPossibleDivisor = (double)BigIntegerSqrt(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){
@@ -593,21 +604,36 @@ namespace mee{
return sum;
}
public static int GetProd(List<int> ary){
int prod = 1;
//If a blank list was passed to the function return 0 as the product
if(ary.Capacity == 0){
return 0;
}
int prod = 1; //Start at 1 because x * 1 = x
foreach(int num in ary){
prod *= num;
}
return prod;
}
public static long GetProd(List<long> ary){
long prod = 1;
//If a blank list was passed to the function return 0 as the product
if(ary.Capacity == 0){
return 0;
}
long prod = 1; //Start at 1 because x * 1 = x
foreach(long num in ary){
prod *= num;
}
return prod;
}
public static BigInteger GetProd(List<BigInteger> ary){
BigInteger prod = 1;
//If a blank list was passed to the function return 0 as the product
if(ary.Capacity == 0){
return 0;
}
BigInteger prod = 1; //Start at 1 because x * 1 = x
foreach(BigInteger num in ary){
prod *= num;
}