-1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MindStudio
  • 706
  • 1
  • 4
  • 13
  • I did google and reviewed different algorithms, but I didn't quite understand any of them. – MindStudio Aug 28 '20 at 12:33
  • Did you read this? https://en.wikipedia.org/wiki/Primality_test – ziggystar Aug 28 '20 at 12:36
  • @ziggystar yes, but I can't seem to find an algorithm that I can easily implement in dart. Also there is the difficulty that there isn't a log() function for BigInts. – MindStudio Aug 28 '20 at 13:12

1 Answers1

0

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

MindStudio
  • 706
  • 1
  • 4
  • 13