SultanC
  1. Home
  2. Examples
  3. Algorithms

Sieve of Eratosthenes

every prime below 50.

algorithms/sieve.sn
// Sieve of Eratosthenes: every prime below 50.
import std.io.println;
import std.text.number;
import std.text.transform;

def main(): int {
    let limit: uint = 50;
    let composite: mutable vector bool = [];
    let i: mutable uint = 0;
    while (i < limit) { append(composite, false); i := i + 1; }
    let primes: mutable vector string = [];
    let candidate: mutable uint = 2;
    while (candidate < limit) {
        if (!composite[candidate]) {
            append(primes, uint_to_text(candidate));
            let multiple: mutable uint = candidate * candidate;
            while (multiple < limit) {
                composite[multiple] := true;
                multiple := multiple + candidate;
            }
        };
        candidate := candidate + 1;
    }
    let space: string = " ";
    println(join(&primes, &space));
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/algorithms/sieve.sn

Or compile it to an executable:

./build/sultanc examples/en/algorithms/sieve.sn -o build/example && ./build/example

Source on GitHub   All examples