scala> lazy val primeS: Stream[Int] = 2 #:: primeS.map(i => Stream.from(i + 1).find(j => primeS.takeWhile(k => k * k <= j).forall(j % _ > 0)).get)

scala> primeS(187060)

res0: Int = 2558951 ]]>

_ < scala.math.sqrt(last).toInt+1

]]>Which made me wonder, what if somebody was willing to sacrifice functional purity, could they get something more concise and efficient? Here’s the best I could come up with:

scala> val primes: Stream[Int] = {

| var last = 2

| 2 #:: Stream.from(last+1).filter(candidate => {

| last=candidate

| primes.takeWhile(_ < last/2+1).forall(candidate % _ != 0)

| })

| }

primes: Stream[Int] = Stream(2, ?)

```
```

`scala> primes.drop(18706).head`

res57: Int = 208843