Is this the most elegant way to express a isPrime function for BigInt objects?
Here’s what I have for regular integers:
def isPrimeForInt(n: Int): Boolean = {
val ceiling = math.sqrt(n.toDouble).toInt
(2 until ceiling) forall (x => n % x != 0)
}
Here’s what I have for BigInts:
def isPrimeForBigInt(n: BigInt): Boolean = {
def ceiling: BigInt = {
def f(a: BigInt): Stream[BigInt] = a #:: f(a+1)
f(BigInt(1)).dropWhile(_.pow(2) < n)(0)
}
Range.BigInt(BigInt(2), ceiling , BigInt(1)) forall (x => n % x != 0)
}
Here is my primality checker for BigInts:
You can read more about it at my Programming with Prime Numbers essay.