How to resolve the algorithm Sieve of Eratosthenes step by step in the Tailspin programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sieve of Eratosthenes step by step in the Tailspin 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 Tailspin programming language
Source code in the tailspin programming language
templates sieve
def limit: $;
@: [ 2..$limit ];
1 -> #
$@ !
when <..$@::length ?($@($) * $@($) <..$limit>)> do
templates sift
def prime: $;
@: $prime * $prime;
@sieve: [ $@sieve... -> # ];
when <..~$@> do
$ !
when <$@~..> do
@: $@ + $prime;
$ -> #
end sift
$@($) -> sift !
$ + 1 -> #
end sieve
1000 -> sieve ...-> '$; ' -> !OUT::write
templates sieve
def limit: $;
@: [ 1..$limit -> 1 ];
@(1): 0;
2..$limit -> #
$@ -> \[i](<=1> $i !\) !
when ($@($) <=1>)> do
def prime2: $ * $;
$prime2..$limit:$ -> @sieve($): 0;
end sieve
1000 -> sieve... -> '$; ' -> !OUT::write
You may also check:How to resolve the algorithm Loops/Foreach step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Fermat numbers step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Additive primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Parameterized SQL statement step by step in the Scala programming language