1# ================================================================
2# Sieve of Eratosthenes: simple example of Miller DSL as programming language.
3# ================================================================
4
5# Put this in a begin-block so we can do either
6#   mlr -n put -q -f name-of-this-file.mlr
7# or
8#   mlr -n put -q -f name-of-this-file.mlr -e '@n = 200'
9# i.e. 100 is the default upper limit, and another can be specified using -e.
10begin {
11  @n = 100;
12}
13
14end {
15  for (int i = 0; i <= @n; i += 1) {
16    @s[i] = true;
17  }
18  @s[0] = false; # 0 is neither prime nor composite
19  @s[1] = false; # 1 is neither prime nor composite
20  # Strike out multiples
21  for (int i = 2; i <= @n; i += 1) {
22    for (int j = i+i; j <= @n; j += i) {
23      @s[j] = false;
24    }
25  }
26  # Print survivors
27  for (int i = 0; i <= @n; i += 1) {
28    if (@s[i]) {
29      print i;
30    }
31  }
32}
33