miller/test/input/sieve.mlr
2021-11-11 14:15:13 -05:00

31 lines
818 B
Text

# ================================================================
# Sieve of Eratosthenes: simple example of Miller DSL as programming language.
# ================================================================
# Put this in a begin-block so we can do either
# mlr -n put -q -f name-of-this-file.mlr
# or
# mlr -n put -q -s n=1000 -f name-of-this-file.mlr
# i.e. 100 is the default upper limit, and another can be specified using -e.
begin {
@n ??= 100;
}
end {
for (int i = 1; i <= @n; i += 1) {
@s[i] = true;
}
@s[1] = false; # 1 is neither prime nor composite
# Strike out multiples
for (int i = 2; i <= @n; i += 1) {
for (int j = i+i; j <= @n; j += i) {
@s[j] = false;
}
}
# Print survivors
for (int i = 1; i <= @n; i += 1) {
if (@s[i]) {
print i;
}
}
}