How to resolve the algorithm Sieve of Eratosthenes step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

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

Eratosthenes Sieve Algorithm in Wolfram Language

The given Wolfram code implements the Eratosthenes sieve algorithm to find all prime numbers up to a given integer n. The sieve algorithm works by iteratively marking off multiples of each prime number starting at 2.

First Implementation:

Eratosthenes[n_] := Module[{numbers = Range[n]},
 Do[If[numbers[[i]] != 0, Do[numbers[[i j]] = 0, {j, 2, n/i}]], {i, 2, Sqrt[n]}];
 Select[numbers, # > 1 &]]

Explanation:

  1. Initialize numbers as a list of integers from 1 to n.
  2. Iterate from 2 to the square root of n. For each i in this range:
    • If numbers[[i]] is not 0 (meaning it hasn't been marked off), it is a prime number.
    • Mark off all multiples of i in the numbers list.
  3. Use Select to filter out the non-prime numbers (# > 1 & condition) and return a list of prime numbers.

Second Implementation:

Eratosthenes[n_] := Module[{numbers = Range[n]},
 Do[If[numbers[[i]] != 0, Do[numbers[[j]] = 0, {j,i i,n,i}]], {i,2,Sqrt[n]}];
 Select[numbers, # > 1 &]]

Explanation:

This implementation is similar to the first one but uses a different way to generate multiples of i. It iterates through the remaining non-marked numbers starting from i i with a step size of i.

Third Implementation:

Eratosthenes2[n_] := Module[{numbers = Range[3, n, 2], limit = (n - 1)/2}, 
 Do[c = numbers[[i]]; If[c != 0,
   Do[numbers[[j]] = 0, {j,(c c - 1)/2,limit,c}]], {i,1,(Sqrt[n] - 1)/2}];
 Prepend[Select[numbers, # > 1 &], 2]]

Explanation:

This implementation optimizes the algorithm by considering only odd numbers (since even numbers greater than 2 are not prime). It initializes numbers as a list of odd numbers from 3 to n. The limit variable is calculated to avoid unnecessary iterations.

  1. Iterate from 1 to (Sqrt[n] - 1)/2. For each i:
    • Let c be the corresponding odd number in the numbers list.
    • If c is not 0 (meaning it hasn't been marked off), mark off all multiples of c in the numbers list.
  2. Prepend the number 2 to the list of selected prime numbers because it is a special case and is not included in the odd number range.

Source code in the wolfram programming language

Eratosthenes[n_] := Module[{numbers = Range[n]},
  Do[If[numbers[[i]] != 0, Do[numbers[[i j]] = 0, {j, 2, n/i}]], {i, 
    2, Sqrt[n]}];
  Select[numbers, # > 1 &]]

Eratosthenes[100]

Eratosthenes[n_] := Module[{numbers = Range[n]},
  Do[If[numbers[[i]] != 0, Do[numbers[[j]] = 0, {j,i i,n,i}]],{i,2,Sqrt[n]}];
  Select[numbers, # > 1 &]]

Eratosthenes[100]

Eratosthenes2[n_] := Module[{numbers = Range[3, n, 2], limit = (n - 1)/2}, 
  Do[c = numbers[[i]]; If[c != 0,
    Do[numbers[[j]] = 0, {j,(c c - 1)/2,limit,c}]], {i,1,(Sqrt[n] - 1)/2}];
  Prepend[Select[numbers, # > 1 &], 2]]

Eratosthenes2[100]

  

You may also check:How to resolve the algorithm Generic swap step by step in the Axe programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Dyalect programming language
You may also check:How to resolve the algorithm String concatenation step by step in the PHP programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Whitespace programming language
You may also check:How to resolve the algorithm Function definition step by step in the Klingphix programming language