# ================================================================
# Sieve of Eratosthenes: simple example of Miller DSL as programming language.
#
# Invocation examples:
#
#   mlr script sieve2.mlr
#   mlr script -e 'n=1000' -f sieve2.mlr
#
# ================================================================

n ??= 100;
s = [];

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;
  }
}
