Пример #1
0
 internal void Add(ChineseRemainder ToAdd)
 {
     for (int Count = 0; Count < DigitsArraySize; Count++)
     {
         // Operations like this could be very fast if
         // they were done on a GPU processor.
         // They could be done in parallel, which would
         // make it a lot faster than the way this is
         // done, one digit at a time.  Notice that
         // there is no carry operation here.
         DigitsArray[Count] += ToAdd.DigitsArray[Count];
         int Prime = (int)IntMath.GetPrimeAt(Count);
         if (DigitsArray[Count] >= Prime)
         {
             DigitsArray[Count] -= Prime;
         }
         // DigitsArray[Count] = DigitsArray[Count] % Prime;
     }
 }
Пример #2
0
        internal bool IsFermatPrime(Integer ToTest, int HowMany)
        {
            // Use bigger primes for Fermat test because the
            // modulus can't be too small.  And also it's
            // more likely to be congruent to 1 with a very
            // small modulus.  In other words it's a lot more
            // likely to appear to be a prime when it isn't.
            // This Fermat primality test is usually
            // described as using random primes to test with,
            // and you could do it that way too.
            // A common way of doing this is to use a multiple
            // of several primes as the base, like
            // 2 * 3 * 5 * 7 = 210.

            int PArrayLength = IntMath.GetPrimeArrayLength();

            if (PArrayLength < 2 * (1024 * 16))
            {
                throw(new Exception("The PrimeArray length is too short for doing IsFermatPrime()."));
            }

            int StartAt = PArrayLength - (1024 * 16); // Or much bigger.

            if (StartAt < 100)
            {
                StartAt = 100;
            }

            for (int Count = StartAt; Count < (HowMany + StartAt); Count++)
            {
                if (!IsFermatPrimeForOneValue(ToTest, IntMath.GetPrimeAt(Count)))
                {
                    return(false);
                }
            }

            // It _might_ be a prime if it passed this test.
            // Increasing HowMany increases the probability that it's a prime.
            return(true);
        }