How to resolve the algorithm Sieve of Eratosthenes step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sieve of Eratosthenes step by step in the D programming language
Table of Contents
Problem Statement
The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer.
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sieve of Eratosthenes step by step in the D programming language
Source code in the d programming language
import std.stdio, std.algorithm, std.range, std.functional;
uint[] sieve(in uint limit) nothrow @safe {
if (limit < 2)
return [];
auto composite = new bool[limit];
foreach (immutable uint n; 2 .. cast(uint)(limit ^^ 0.5) + 1)
if (!composite[n])
for (uint k = n * n; k < limit; k += n)
composite[k] = true;
//return iota(2, limit).filter!(not!composite).array;
return iota(2, limit).filter!(i => !composite[i]).array;
}
void main() {
50.sieve.writeln;
}
import std.stdio, std.math, std.array;
size_t[] sieve(in size_t m) pure nothrow @safe {
if (m < 3)
return null;
immutable size_t n = m - 1;
enum size_t bpc = size_t.sizeof * 8;
auto F = new size_t[((n + 2) / 2) / bpc + 1];
F[] = size_t.max;
size_t isSet(in size_t i) nothrow @safe @nogc {
immutable size_t offset = i / bpc;
immutable size_t mask = 1 << (i % bpc);
return F[offset] & mask;
}
void resetBit(in size_t i) nothrow @safe @nogc {
immutable size_t offset = i / bpc;
immutable size_t mask = 1 << (i % bpc);
if ((F[offset] & mask) != 0)
F[offset] = F[offset] ^ mask;
}
for (size_t i = 3; i <= sqrt(real(n)); i += 2)
if (isSet((i - 3) / 2))
for (size_t j = i * i; j <= n; j += 2 * i)
resetBit((j - 3) / 2);
Appender!(size_t[]) result;
result ~= 2;
for (size_t i = 3; i <= n; i += 2)
if (isSet((i - 3) / 2))
result ~= i;
return result.data;
}
void main() {
50.sieve.writeln;
}
/// Extensible Sieve of Eratosthenes.
struct Prime {
uint[] a = [2];
private void grow() pure nothrow @safe {
immutable p0 = a[$ - 1] + 1;
auto b = new bool[p0];
foreach (immutable di; a) {
immutable uint i0 = p0 / di * di;
uint i = (i0 < p0) ? i0 + di - p0 : i0 - p0;
for (; i < b.length; i += di)
b[i] = true;
}
foreach (immutable uint i, immutable bi; b)
if (!b[i])
a ~= p0 + i;
}
uint opCall(in uint n) pure nothrow @safe {
while (n >= a.length)
grow;
return a[n];
}
}
version (sieve_of_eratosthenes3_main) {
void main() {
import std.stdio, std.range, std.algorithm;
Prime prime;
uint.max.iota.map!prime.until!q{a > 50}.writeln;
}
}
You may also check:How to resolve the algorithm Compound data type step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the C++ programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Entropy step by step in the Perl programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Run BASIC programming language