Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Created March 16, 2013 13:57
Show Gist options
  • Save tpolecat/5176507 to your computer and use it in GitHub Desktop.
Save tpolecat/5176507 to your computer and use it in GitHub Desktop.
object Ishi extends App {
class Primes {
val primes: Stream[Long] = 2 #:: primesStartingFrom(3)
def isPrime(n: Long): Boolean = {
val limit = math.sqrt(n).ceil.toInt
!primes.takeWhile(_ < limit).exists(n % _ == 0)
}
private def primesStartingFrom(n: Int): Stream[Long] = {
if (isPrime(n)) {
n #:: primesStartingFrom(n + 2)
} else {
primesStartingFrom(n + 2)
}
}
}
val primes = new Primes
Iterator.iterate(3L)(_ + 2).takeWhile(_ < 490L).grouped(2).foreach(is => {
is.par.foreach(i => {
println("Checking: " + i)
if (primes.isPrime(i)) {
println(i)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment