public static bool IsProbablyPrime(this long prime, int certainty, Ibasa.Numerics.Random.Generator random) { if (prime == 2 || prime == 3) return true; if (prime < 2 || prime % 2 == 0) return false; int s = 0; var d = prime - 1; while (d % 2 == 0) { d /= 2; s += 1; } for (int i = 0; i < certainty; ++i) { long a = random.Next(2, prime - 2); long x = Ibasa.Numerics.Functions.ModPow(a, d, prime); if (x != 1 && x != prime - 1) { for (int r = 1; r < s; ++r) { x = Ibasa.Numerics.Functions.ModPow(x, 2, prime); if (x == 1) return false; if (x == prime - 1) break; } if (x != prime - 1) return false; } } return true; }