Sieve

The Sieve of Eratosthenes

Backed by a bool[]; the template argument is only used for integral type yielded by the range.

Constructors

this
this(size_t length)

Constructs a sieve with the given size.

Members

Functions

fixed
auto fixed()

Return a finite slice of the sieve.

front
T front()
Undocumented in source. Be warned that the author may not have intended to support it.
popFront
void popFront()
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

empty
bool empty;
Undocumented in source.

Examples

import std.algorithm : sum, until;
import std.range : take, array;

assert(Sieve!int(50).until!(n => n >= 50).sum == 328);
assert(Sieve!int(nthPrimeUpperBound(50)).take(50).sum == 5117);
assert(Sieve!int(30).fixed.array == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
assert(Sieve!int(0).fixed.array == []);

// this causes compilation to take about 9s and 2 GB of memory.
// static assert(Sieve!ulong(2_000_000).fixed.sum = 142913828922);

auto ps = Sieve!int();
assert(ps.front == 2);
ps.popFront();
assert(ps.front == 3);

Meta