How to resolve the algorithm Sieve of Eratosthenes step by step in the Woma programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sieve of Eratosthenes step by step in the Woma 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 Woma programming language
Source code in the woma programming language
(sieve(n = /0 -> int; limit = /0 -> int; is_prime = [/0] -> *)) *
i<@>range(n*n, limit+1, n)
is_prime = is_prime[$]i,False
<*>is_prime
(primes_upto(limit = 4 -> int)) list(int)
primes = [] -> list
f = [False, False] -> list(bool)
t = [True] -> list(bool)
u = limit - 1 -> int
tt = t * u -> list(bool)
is_prime = flatten(f[^]tt) -> list(bool)
limit_sqrt = limit ** 0.5 -> float
iter1 = int(limit_sqrt + 1.5) -> int
n<@>range(iter1)
is_prime[n]>is_prime = sieve(n, limit, is_prime)
i,prime<@>enumerate(is_prime)
prime>primes = primes[^]i
<*>primes
You may also check:How to resolve the algorithm Kronecker product step by step in the F# programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Time a function step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the Sidef programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the LOLCODE programming language