Skip to content

Instantly share code, notes, and snippets.

@ramn
Last active September 19, 2019 08:14
Show Gist options
  • Save ramn/8378315 to your computer and use it in GitHub Desktop.
Save ramn/8378315 to your computer and use it in GitHub Desktop.
Prime generator in Scala
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
@sgabber
Copy link

sgabber commented Sep 19, 2019

Cool! primes being never multiples of 2 could speed up a little write Stream.from(3,step=2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment