From fa5b4d2f150f085497256b5a084332f6df70bbf2 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 11 Mar 2021 13:35:04 -0500 Subject: [PATCH] Fixed bug in getting BigInteger sqrts --- CSClasses/Algorithms.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index e073d85..3027249 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -29,6 +29,12 @@ using System.Numerics; namespace mee{ public class Algorithms{ + //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)); + } + //These functions return a list of all Fibonacci numbers <= goalNumber public static List GetAllFib(int goalNumber){ //Setup the variables @@ -304,7 +310,7 @@ namespace mee{ //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); + BigInteger topPossibleFactor = BigIntegerSqrt(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){ @@ -440,7 +446,7 @@ namespace mee{ //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); + BigInteger topPossibleFactor = BigIntegerSqrt(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){