mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Fixed bug in getting BigInteger sqrts
This commit is contained in:
@@ -29,6 +29,12 @@ using System.Numerics;
|
|||||||
|
|
||||||
namespace mee{
|
namespace mee{
|
||||||
public class Algorithms{
|
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
|
//These functions return a list of all Fibonacci numbers <= goalNumber
|
||||||
public static List<int> GetAllFib(int goalNumber){
|
public static List<int> GetAllFib(int goalNumber){
|
||||||
//Setup the variables
|
//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
|
//We can now start at 3 and skip all even numbers, because they cannot be prime
|
||||||
for(BigInteger possiblePrime = 3;possiblePrime <= goalNumber;possiblePrime += 2){
|
for(BigInteger possiblePrime = 3;possiblePrime <= goalNumber;possiblePrime += 2){
|
||||||
//Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
//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
|
//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;){
|
for(int primesCnt = 0;primes[primesCnt] <= topPossibleFactor;){
|
||||||
if((possiblePrime % primes[primesCnt]) == 0){
|
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
|
//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){
|
for(BigInteger possiblePrime = 3;primes.Count < numberOfPrimes;possiblePrime += 2){
|
||||||
//Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
//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
|
//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;){
|
for(int primesCnt = 0;primes[primesCnt] <= topPossibleFactor;){
|
||||||
if((possiblePrime % primes[primesCnt]) == 0){
|
if((possiblePrime % primes[primesCnt]) == 0){
|
||||||
|
|||||||
Reference in New Issue
Block a user