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

Published on 12 May 2024 09:40 PM

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

Source code in the comal programming language

// Sieve of Eratosthenes
input "Limit? ": limit
dim sieve(1:limit)
sqrlimit:=sqr(limit)
sieve(1):=1
p:=2
while p<=sqrlimit do
 while sieve(p) and p
  p:=p+1
 endwhile
 if p>sqrlimit then goto done
 for i:=p*p to limit step p do
  sieve(i):=1
 endfor i
 p:=p+1
endwhile
done:
print 2,
for i:=3 to limit do
 if sieve(i)=0 then
  print ", ",i,
 endif
endfor i
print


  

You may also check:How to resolve the algorithm Empty program step by step in the Io programming language
You may also check:How to resolve the algorithm Jordan-Pólya numbers step by step in the J programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the jq programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Ring programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the Tcl programming language