← 都来码 / Dolaima← 例程索引kotlin/school/sieve.kt 原文

kotlin/school/sieve.kt · 426 字节 · 下载原文

// source: https://rosettacode.org/wiki/Sieve_of_Eratosthenes
// license: GFDL 1.2 (Rosetta Code)
// fetched: 2026-10-14 (Rosetta Code "Kotlin" section, verbatim)
import kotlin.math.sqrt

fun sieve(max: Int): List<Int> {
    val xs = (2..max).toMutableList()
    val limit = sqrt(max.toDouble()).toInt()
    for (x in 2..limit) xs -= x * x..max step x
    return xs
}

fun main(args: Array<String>) {
    println(sieve(100))
}