Created
March 16, 2013 13:57
-
-
Save tpolecat/5176507 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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