fork download
  1. object Main extends App {
  2. def isPrime(n: Int): Boolean =
  3. n>1 && (2 to Math.sqrt(n).toInt).toList.forall(n%_!=0)
  4.  
  5. def primeStream(n: Int): Stream[Int] = isPrime(n) match{
  6. case true => n#::primeStream(n+1)
  7. case _ => primeStream(n+1)
  8. }
  9.  
  10. val primes = primeStream(0)
  11. println(primes.take(readInt).toList)
  12. }
Success #stdin #stdout 0.16s 322496KB
stdin
11
stdout
List(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)