Kotlin sieve using sequences
Here's my take on this:

import kotlin.system.measureTimeMillis

fun sieve(xs: Sequence&lt;Int&gt;): Sequence&lt;Int&gt; = sequence {
    val head = xs.first()
    val tail = xs.drop(1).filter { it % head != 0 }
    yield(head)
    for (i in sieve(tail))
        yield(i)
}

val primes = sieve(generateSequence(2) { it + 1 })
fun isPrime(n: Int) = primes.contains(n)

val durationMs = measureTimeMillis {
    println("isPrime(4057) = ${isPrime(4057)}")
}
println("Duration = $durationMs ms")



Start with the first element in the sequence - number 2
Filter out all numbers divisible by 2
Yield 2
Yield the rest recursively:


Start with the first element in the sequence - number 3
Filter out all numbers divisible by 3
Yield 3
Yield the rest recursively...