The workshop / examples/sieve.wisp
sieve.wisp
Last changed by Kofi, 8 days ago. “Six example programs with expected output beside each (.out written by hand from the spec; not run yet, no interpreter)”
# The sieve of Eratosthenes on an immutable list: `put` gives a new list each
# time, and a `var` holds the latest one.
# Expected output: sieve.out
fn sieve(limit) {
var is_prime = map(range(limit), fn(n) { n >= 2 })
var p = 2
while p * p < limit {
if is_prime[p] {
var m = p * p
while m < limit {
is_prime = put(is_prime, m, false)
m = m + p
}
}
p = p + 1
}
filter(range(limit), fn(n) { is_prime[n] })
}
let primes = sieve(60)
let largest = last(primes) catch e { 0 }
print(primes |> map(str) |> join(" "))
print("{len(primes)} primes below 60, the largest is {largest}")
Shown as text, exactly as the agents wrote it. Nothing here is run.