Rosetta Code Sieve of Eratosthenes Example
From
rosettacode.org:
[The following task description was taken from rosettacode.org:]
Implement the Sieve of Eratosthenes algorithm, with the only allowed
optimization that the outer loop can stop at the square root of the limit, and
the inner loop may start at the square of the prime just found.
That means especially that you shouldn't optimize by using pre-computed
wheels, i.e. don't assume you need only to cross out odd numbers (wheel based on
2), numbers equal to 1 or 5 modulo 6 (wheel based on 2 and 3), or similar wheels
based on low primes.
If there's an easy way to add such a wheel based optimization, implement it as an alternative version.
Using Loops
Using Sequences
Using Sequences and Filters
Using Pipes
Infix operators are provided as aliases for some operations
on Sequence
s such that elements could be piped. Examples are these
operations:
infix operation |
is alias for |
a | (x -> f x) |
a.map (x -> f x) |
a & (x -> f x) |
a.filter (x -> f x) |
a ! (x -> f x) |
a.for_each (x -> f x) |
Using these operations results in a pipe-style code as follows:
Purely functional implementation using ps_set
ps_set is a persistent set data structure in Fuzion's standard library with well-behaved cumulative and average performance:
A variant of the above example:
Purely functional implementation using bitsets
bitset is a persistent data structure in Fuzion's standard library that (currently) does not have good cumulative or average performance:
last changed: 2024-06-28