How do I check the primality of a very large BigInt fast?

169 views Asked by At

I am generating a large BigInt and need to test whether it is a prime number or not. However, this simple algorithm takes way to long.

bool _isProbablePrime(BigInt n) {
  var _isPrime = true;
  for (var i = BigInt.from(2); i < (n); i = i + BigInt.one) {
    if (n % i == BigInt.zero) _isPrime = false;
  }
  return _isPrime;
}

Is there another more efficient algorithm that has a fairly high certainty of correctly checking the primality of n?

Any help is appreciated, and thanks in advance! :D

1

There are 1 answers

0
MindStudio On

For everyone interested: The simplest solutions seems to be to use an implementation of the Miller-Rabin primality test.

There is a dart-package called ninja_prime that uses this algorithm to determin the primality of BigInts. https://pub.dev/packages/ninja_prime