From 4ac0db8d6d1804d0a8314fc662d27e410267778e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 11 Mar 2021 14:53:45 -0500 Subject: [PATCH] Various bug fixes --- CSClasses/Algorithms.cs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 3027249..09b3ddf 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -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 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 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 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; }