public BigInteger NextPrime(int sizeBytes) { BigInteger testNum = Next(sizeBytes); if (testNum % 2 == 0) { testNum--; } BigInteger current = testNum; bool failed = false; while (!BigIntegerPrimeTest.MillerRabinTest(current)) { if (current.ToByteArray().Length == sizeBytes) { current -= 2; } else { failed = true; break; } } if (!failed) { return(current); } else { current = testNum + 2; while (!BigIntegerPrimeTest.MillerRabinTest(current)) { current += 2; } } return(current); }
//Ро- метод Полларда public static List <BigInteger> PollardsAlg(BigInteger n) { var result = new List <BigInteger>(); if (BigIntegerPrimeTest.MillerRabinTest(n)) { return(result); } BigInteger N = n; BigInteger i = 1; BigInteger nextiToSave = 1; Random rand = new Random(); BigInteger x = RandomBigInteger(n, rand); BigInteger y = 1, lastx = 1; BigInteger res = 0; while (N % 2 == 0) { if (!result.Contains(2)) { result.Add(2); } N /= 2; } while (!BigIntegerPrimeTest.MillerRabinTest(N)) { BigInteger factor; BigInteger delta = BigInteger.Abs(x - y); if (delta == 0) { x = RandomBigInteger(n, rand); i = 0; nextiToSave = 1; y = 1; lastx = 1; continue; } BigInteger gcd = BigInteger.GreatestCommonDivisor(N, delta); lastx = x; x = (x * x - 1) % n; if (i == nextiToSave) { nextiToSave *= 2; y = lastx; } i++; if (gcd > 1) { factor = gcd; if (!BigIntegerPrimeTest.MillerRabinTest(factor)) { var factorFactors = PollardsAlg(factor); foreach (var item in factorFactors) { result.Add(item); } } else { if (factor > 1) { result.Add(factor); } } N /= factor; } } if (N > 1 && !result.Contains(N)) { result.Add(N); } result.Sort(); return(result); }