How to resolve the algorithm Sieve of Eratosthenes step by step in the SETL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sieve of Eratosthenes step by step in the SETL 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 SETL programming language

Source code in the setl programming language

program eratosthenes;
    print(sieve 100);

    op sieve(n);
        numbers := [1..n];
        numbers(1) := om;
        loop for i in [2..floor sqrt n] do
            loop for j in [i*i, i*i+i..n] do
                numbers(j) := om;
            end loop;
        end loop;
        return [n : n in numbers | n /= om];
    end op;
end program;

  

You may also check:How to resolve the algorithm Delete a file step by step in the Java programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the Factor programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Quackery programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Miranda programming language
You may also check:How to resolve the algorithm Peano curve step by step in the AutoHotkey programming language