Not having a computer science background, I was curious about how to write a Scala function that would find a list of all prime numbers up until some maximum value I supply. I was aware of the Sieve of Eratosthenes, but I didn’t want to implement that, at least not today.
A Scala 3 (Dotty) function
What I ended up doing looks like other Scala prime number solutions you can find on the internet. To make things look a little different, I wrote the code using the current Scala 3 (Dotty) syntax, and the result looks like this:
def isPrime(i: Int): Boolean = if (i <= 1) false else if (i == 2) true else !(2 until i).exists(n => i % n == 0)