object Main extends App { def isPrime(n: Int): Boolean = n>1 && (2 to Math.sqrt(n).toInt).toList.forall(n%_!=0) def primeStream(n: Int): Stream[Int] = isPrime(n) match{ case true => n#::primeStream(n+1) case _ => primeStream(n+1) } val primes = primeStream(0) println(primes.take(readInt).toList)}
11
List(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!