Fixed bug in getting BigInteger sqrts

This commit is contained in:
2021-03-11 13:35:04 -05:00
parent 32a937f626
commit fa5b4d2f15

View File

@@ -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<int> 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){