public static bool IsProbablePrime(this BigInteger input, int number_of_iterations) { //TODO could be paralel if (input == 2 || input == 3) { return(true); } if (input < 2 || input % 2 == 0) { return(false); } BigInteger power = input - 1; int trailing_count = 0; while ((power % 2 == 0) && (power != 0)) { power /= 2; trailing_count += 1; } RandomNumberGenerator random = new RNGCryptoServiceProvider(); for (int index_witness = 0; index_witness < number_of_iterations; index_witness++) { BigInteger witness = random.RandomPositiveBigIntegerBelow(input); if (ToolsMathBigIntegerPrime.IsCompositeByMillerRabinWitness(input, witness, trailing_count, power)) { return(false); } } return(true); }