How to resolve the algorithm Sieve of Eratosthenes step by step in the Limbo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sieve of Eratosthenes step by step in the Limbo 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 Limbo programming language
Source code in the limbo programming language
implement Sieve;
include "sys.m";
sys: Sys;
print: import sys;
include "draw.m";
draw: Draw;
Sieve : module
{
init : fn(ctxt : ref Draw->Context, args : list of string);
};
init (ctxt: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
limit := 201;
sieve : array of int;
sieve = array [201] of {* => 1};
(sieve[0], sieve[1]) = (0, 0);
for (n := 2; n < limit; n++) {
if (sieve[n]) {
for (i := n*n; i < limit; i += n) {
sieve[i] = 0;
}
}
}
for (n = 1; n < limit; n++) {
if (sieve[n]) {
print ("%4d", n);
} else {
print(" .");
};
if ((n%20) == 0)
print("\n\n");
}
}
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Input loop step by step in the Haskell programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Batch File programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the Perl programming language
You may also check:How to resolve the algorithm Digital root step by step in the S-BASIC programming language