isPrime method

bool isPrime()

Returns whether this number is prime.

A prime number is a natural number greater than 1 that is only divisible by 1 and itself. This implementation uses trial division with optimization for factors of 2 and 3.

Implementation

bool isPrime() {
  if (this <= 1) return false;
  if (this <= 3) return true;
  if (isDivisibleBy(2) || isDivisibleBy(3)) return false;

  for (var i = 5; i * i <= this; i += 6) {
    if (isDivisibleBy(i) || isDivisibleBy(i + 2)) return false;
  }
  return true;
}