fun sieve(n: Int): List<Boolean> {
var primes = mutableListOf(false, false)
for (i in 2 until n + 1) {
primes.add(true)
}
for (i in 2 until n) {
if (!primes[i]) {
continue
}
for (j in 2 * i until n + 1 step i) {
primes[j] = false
}
}
return primes
}
fun printPrimeNumbers(primes: List<Boolean>) {
for (i in 0 until primes.count()) {
if (primes[i]) {
println(i)
}
}
}
fun main() {
val n = 100
val primes = sieve(n)
println("Liczby pierwsze od 1 do $n:")
printPrimeNumbers(primes)
}
ZnVuIHNpZXZlKG46IEludCk6IExpc3Q8Qm9vbGVhbj4gewogIHZhciBwcmltZXMgPSBtdXRhYmxlTGlzdE9mKGZhbHNlLCBmYWxzZSkKCiAgZm9yIChpIGluIDIgdW50aWwgbiArIDEpIHsKICAgIHByaW1lcy5hZGQodHJ1ZSkKICB9CgogIGZvciAoaSBpbiAyIHVudGlsIG4pIHsKICAgIGlmICghcHJpbWVzW2ldKSB7CiAgICAgIGNvbnRpbnVlCiAgICB9CgogICAgZm9yIChqIGluIDIgKiBpIHVudGlsIG4gKyAxIHN0ZXAgaSkgewogICAgICBwcmltZXNbal0gPSBmYWxzZQogICAgfQogIH0KCiAgcmV0dXJuIHByaW1lcwp9CgpmdW4gcHJpbnRQcmltZU51bWJlcnMocHJpbWVzOiBMaXN0PEJvb2xlYW4+KSB7CiAgZm9yIChpIGluIDAgdW50aWwgcHJpbWVzLmNvdW50KCkpIHsKICAgIGlmIChwcmltZXNbaV0pIHsKICAgICAgcHJpbnRsbihpKQogICAgfQogIH0KfQoKZnVuIG1haW4oKSB7CiAgdmFsIG4gPSAxMDAKCiAgdmFsIHByaW1lcyA9IHNpZXZlKG4pCgogIHByaW50bG4oIkxpY3pieSBwaWVyd3N6ZSBvZCAxIGRvICRuOiIpCiAgcHJpbnRQcmltZU51bWJlcnMocHJpbWVzKQp9