Last active
September 19, 2019 08:14
-
-
Save ramn/8378315 to your computer and use it in GitHub Desktop.
Prime generator in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val primes: Stream[Int] = 2 #:: Stream.from(3).filter { n => !primes.takeWhile(_ <= math.sqrt(n)).exists(n % _ == 0) } | |
def isPrime(n: Int) = primes.dropWhile(_ < n).head == n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool! primes being never multiples of 2 could speed up a little write
Stream.from(3,step=2)