How to resolve the algorithm The sieve of Sundaram step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm The sieve of Sundaram step by step in the Mathematica/Wolfram Language programming language

Table of Contents

Problem Statement

The sieve of Eratosthenes: you've been there; done that; have the T-shirt. The sieve of Eratosthenes was ancient history when Euclid was a schoolboy. You are ready for something less than 3000 years old. You are ready for The sieve of Sundaram. Starting with the ordered set of +ve integers, mark every third starting at 4 (4;7;10...). Step through the set and if the value is not marked output 2*n+1. So from 1 to 4 output 3 5 7. 4 is marked so skip for 5 and 6 output 11 and 13. 7 is marked, so no output but now also mark every fifth starting at 12 (12;17;22...) as per to 10 and now mark every seventh starting at 17 (17;24;31....) as per for every further third element (13;16;19...) mark every (9th;11th;13th;...) element. The output will be the ordered set of odd primes. Using your function find and output the first 100 and the millionth Sundaram prime. The faithless amongst you may compare the results with those generated by The sieve of Eratosthenes. Comment on the Sundaram Sieve In case casual readers and programmers read the above blurb and get the impression that something several thousand years newer must needs be better than the "old" Sieve of Eratosthenes (SoE), do note the only difference between the Sieve of Sundaram (SoS) and the odds-only SoE is that the SoS marks as composite/"culls" according to all odd "base" numbers as is quite clear in the above description of how to implement it and the above linked Wikipedia article (updated), and the SoE marks as composite/"culls" according to only the previously determined unmarked primes (which are all odd except for two, which is not used for the "odds-only" algorithm); the time complexity (which relates to the execution time) is therefore O(n log n) for the SoS and O(n log log n) for the SoE, which difference can make a huge difference to the time it takes to sieve as the ranges get larger. It takes about a billion "culls" to sieve odds-only to a billion for the SoE, whereas it takes about 2.28 billion "culls" to cull to the same range for the SoS, which implies that the SoS must be about this ratio slower for this range with the memory usage identical. Why would one choose the SoS over the SoE to save a single line of code at the cost of this much extra time? The Wren comparison at the bottom of this page makes that clear, as would implementing the same in any language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm The sieve of Sundaram step by step in the Mathematica/Wolfram Language programming language

Code Overview

The provided Wolfram code defines a function called SieveOfSundaram that implements a prime number generation algorithm known as the Sieve of Sundaram. It calculates prime numbers up to a specified limit n.

Algorithm Explanation

The Sieve of Sundaram is an efficient algorithm for generating prime numbers. It starts with a range of integers from 1 to n and then iteratively eliminates composite (non-prime) numbers. The algorithm works as follows:

  1. Initialize range: Start with an array of True values for numbers from 1 to n/2. This represents the potential prime numbers.

  2. Sieve process: For each number i from 1 to n/2:

    • Calculate prefac as 2i + 1.
    • If i + i * prefac is within the range, mark the corresponding number as False in the array. This eliminates composite numbers that can be formed from i and prefac.
  3. Generate primes: Gather the positions of True values in the array. Multiply each position by 2 and add 1 to obtain the list of prime numbers.

Code Details

Function Definition:

ClearAll[SieveOfSundaram]
SieveOfSundaram[n_Integer] := Module[{i, prefac, k, ints},
  • ClearAll[SieveOfSundaram]: Clears the definition of the SieveOfSundaram function to ensure a fresh start.
  • SieveOfSundaram[n_Integer] := Module[{...}]: Defines the SieveOfSundaram function that takes an integer n as a parameter.

Sieving Process:

k = Floor[(n - 2)/2];
ints = ConstantArray[True, k + 1];
Do[
  prefac = 2 i + 1;
  If[i + i prefac <= k,
   ints[[i + i prefac ;; ;; prefac]] = False
   ];
  ,
  {i, 1, k + 1}
  ];
  • k = Floor[(n - 2)/2]: Calculates half of the range of integers to sieve.
  • ints = ConstantArray[True, k + 1]: Initializes the array of potential prime numbers.
  • Do[..., {i, 1, k + 1}]: Iterates through each number i in the range.
  • prefac = 2 i + 1: Calculates the prefac value.
  • If[i + i prefac <= k, ... ]: Checks if i and prefac are within the range. If so, it eliminates composite numbers formed from them.

Prime Generation:

2 Flatten[Position[ints, True]] + 1
  • Position[ints, True]: Finds the positions of True values in the ints array, representing potential prime numbers.
  • Flatten[...]: Combines the lists of positions into a single list.
  • 2 [...] + 1: Multiplies each position by 2 and adds 1 to generate the list of prime numbers.

Usage Examples:

SieveOfSundaram[600][[;; 100]]
SieveOfSundaram[16000000][[10^6]]

These examples demonstrate the usage of the SieveOfSundaram function. The first call generates and prints the first 100 prime numbers up to 600, while the second call prints the 1,000,000th prime number up to the limit of 16,000,000.

Source code in the wolfram programming language

ClearAll[SieveOfSundaram]
SieveOfSundaram[n_Integer] := Module[{i, prefac, k, ints},
  k = Floor[(n - 2)/2];
  ints = ConstantArray[True, k + 1];
  Do[
   prefac = 2 i + 1;
   If[i + i prefac <= k,
    ints[[i + i prefac ;; ;; prefac]] = False
    ];
   ,
   {i, 1, k + 1}
   ];
  2 Flatten[Position[ints, True]] + 1
  ]
SieveOfSundaram[600][[;; 100]]
SieveOfSundaram[16000000][[10^6]]


  

You may also check:How to resolve the algorithm Euler method step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Vala programming language
You may also check:How to resolve the algorithm Longest increasing subsequence step by step in the Swift programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Aikido programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Elixir programming language