class Primes {
  val primes: Stream[Long] = 2 #:: primesStartingFrom(3)

  def isPrime(n: Long): Boolean = {
    println("isPrime " + n)
    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)
    }
  }
}

object Ishi extends Application {
  val primes = new Primes
  Iterator.iterate(3L)(_ + 2).takeWhile(_ < 490L).grouped(4).foreach(is => {
    is.par.foreach(i => {
      println("Checking: " + i)
      if (primes.isPrime(i)) {
        println(i)
      }
    })
  })
}