Last active
December 30, 2021 11:25
-
-
Save domnikl/6ad30f0a6ca3228f44e8273c08dbd879 to your computer and use it in GitHub Desktop.
Kotlin Prime number sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun primes(): Sequence<Long> { | |
var i = 0L | |
return sequence { | |
generateSequence { i++ } | |
.filter { n -> n > 1 && ((2 until n).none { i -> n % i == 0L }) } | |
.forEach { yield(it) } | |
} | |
} | |
println(primes().take(10).joinToString(", ")) |
Hey, looks great, thanks for sharing it!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I have modified it a bit to work faster with larger prime numbers.