Esempio n. 1
0
        public static List <BigInteger> LoadPrimes()
        {
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }
            List <BigInteger> result = new List <BigInteger>();

            try
            {
                using (BinaryReader reader = new BinaryReader(File.Open(basePath + "PrimeDatabase.savedata", FileMode.Open)))
                {
                    int length = reader.ReadInt32();
                    for (int i = 0; i < length; i++)
                    {
                        result.Add(MathExtras.ReadBigInteger(reader));
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("Error loading Primes");
                return(new List <BigInteger>());
            }
            return(result);
        }
        private static List <BigInteger> ReadFactors(BinaryReader reader)
        {
            List <BigInteger> factors = new List <BigInteger>();
            int count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                factors.Add(MathExtras.ReadBigInteger(reader));
            }
            return(factors);
        }
Esempio n. 3
0
 public static void Write(this BigInteger number, BinaryWriter writer)
 {
     MathExtras.WriteBigInteger(number, writer);
 }
Esempio n. 4
0
        public static List <BigInteger> Factorize(this BigInteger number)
        {
            if (number.BitCount > 10 && MathExtras.MillerTest(number))
            {
                // it's prime already!
                return(new List <BigInteger>());
            }
            List <BigInteger> factors = new List <BigInteger>();
            int next = 0;

            while (number > 1)
            {
                if (Program.primes.Count <= next)
                {
                    // out of primes

                    // Calculations concluded that number number is prime
                    if (MathExtras.MillerTest(number))
                    {
                        factors.Add(number);
                        return(factors);
                    }

                    BigInteger sqrt = BigInteger.Sqrt(number);
                    if (sqrt * sqrt == number)
                    {
                        // Calculations concluded that number has two factors of sqrt
                        if (MathExtras.MillerTest(sqrt))
                        {
                            // and they are prime
                            factors.Add(sqrt);
                            factors.Add(sqrt);
                        }
                        else
                        {
                            // and they are composite
                            factors.Add(-sqrt);
                            factors.Add(-sqrt);
                        }
                        continue;
                    }

                    // no more factors can be found
                    factors.Add(-number);
                    return(factors);

                    /*
                     * MathExtras.PrimeFactorizationResult res = MathExtras.PollardsRho(number);
                     * switch (res.result)
                     * {
                     *  case MathExtras.PrimeFactorizationResultEvaluation.FactorFound:
                     *      // Calculations concluded that number has a factor of res.primeFactor
                     *      factors.Add(res.primeFactor);
                     *      number /= res.primeFactor;
                     *      continue;
                     *
                     *  case MathExtras.PrimeFactorizationResultEvaluation.Prime:
                     *      // Calculations concluded that number number is prime
                     *      factors.Add(number);
                     *      return factors;
                     *
                     *  default:
                     *      // There was an issue factorizing
                     *      factors.Add(-number);
                     *      return factors;
                     * }
                     */
                }
                else
                {
                    if (Program.primes[next] < 0)
                    {
                        // ran out of safe primes
                        next = Program.primes.Count;
                        continue;
                    }
                }
                BigInteger prime = Program.primes[next];
                if (number % prime == 0)
                {
                    factors.Add(prime);
                    number /= prime;
                }
                else
                {
                    next += 1;
                }
            }
            return(factors);
        }
Esempio n. 5
0
 public static void Write(this BigFloat number, BinaryWriter writer)
 {
     MathExtras.WriteBigFloat(number, writer);
 }