How to resolve the algorithm Ulam spiral (for primes) step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Ulam spiral (for primes) step by step in the Haskell programming language

Table of Contents

Problem Statement

An Ulam spiral (of primes) is a method of visualizing primes when expressed in a (normally counter-clockwise) outward spiral (usually starting at 1),   constructed on a square grid, starting at the "center". An Ulam spiral is also known as a   prime spiral. The first grid (green) is shown with sequential integers,   starting at   1. In an Ulam spiral of primes, only the primes are shown (usually indicated by some glyph such as a dot or asterisk),   and all non-primes as shown as a blank   (or some other whitespace). Of course, the grid and border are not to be displayed (but they are displayed here when using these Wiki HTML tables). Normally, the spiral starts in the "center",   and the   2nd   number is to the viewer's right and the number spiral starts from there in a counter-clockwise direction. There are other geometric shapes that are used as well, including clock-wise spirals. Also, some spirals (for the   2nd   number)   is viewed upwards from the   1st   number instead of to the right, but that is just a matter of orientation. Sometimes, the starting number can be specified to show more visual striking patterns (of prime densities). [A larger than necessary grid (numbers wise) is shown here to illustrate the pattern of numbers on the diagonals   (which may be used by the method to orientate the direction of spiral-construction algorithm within the example computer programs)]. Then, in the next phase in the transformation of the Ulam prime spiral,   the non-primes are translated to blanks. In the orange grid below,   the primes are left intact,   and all non-primes are changed to blanks.
Then, in the final transformation of the Ulam spiral (the yellow grid),   translate the primes to a glyph such as a   •   or some other suitable glyph.

The Ulam spiral becomes more visually obvious as the grid increases in size.

For any sized   N × N   grid,   construct and show an Ulam spiral (counter-clockwise) of primes starting at some specified initial number   (the default would be 1),   with some suitably   dotty   (glyph) representation to indicate primes,   and the absence of dots to indicate non-primes.
You should demonstrate the generator by showing at Ulam prime spiral large enough to (almost) fill your terminal screen.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ulam spiral (for primes) step by step in the Haskell programming language

The provided code is a Haskell program that uses the Ulam spiral algorithm applied to a specific range of numbers between 1 and 100. It creates a visual representation of the spiral using the Diagrams library and exports the result as an SVG image. Here's a detailed explanation of the code:

  1. Importing Modules:

    • import Data.List: Provides functions for working with lists.
    • import Data.Numbers.Primes: Provides functions for checking primality.
    • import Diagrams.Prelude: Provides the Diagrams library for creating and manipulating diagrams.
    • import Diagrams.Backend.SVG.CmdLine: Allows exporting diagrams as SVG images.
  2. Data Structures:

    • ulam function generates the Ulam spiral for a given range of numbers and a representation function (here, dots). It consists of two steps: swirl and map.
  3. swirl Function:

    • This function takes a number n and creates a spiral pattern by alternately rotating and stacking elements of a list.
    • It first uses chop to divide the list into chunks of size n.
    • Then, it repeatedly rotates the chunks and stacks them on top of each other to form the spiral.
  4. chop Function:

    • This function divides a list into chunks of a specified size.
    • It splits the list into two parts: a chunk of the specified size and the remaining part.
    • It then recursively applies itself to the remaining part to obtain all the chunks.
  5. spool Function:

    • This function takes a list of lists and stacks them together in a spiral pattern.
    • It uses foldl to repeatedly accumulate elements into a table, where each row is rotated by one position to create the spiral effect.
  6. showTable Function:

    • This function converts the table into a string representation, where each row is padded with spaces to ensure a consistent width.
    • It uses foldMap to apply the pad function to each row and then concatenate them into a single string.
  7. dots Function:

    • This function takes a number and creates a diagram.
    • If the number is prime, it creates a black circle, and if it's not prime, it creates a white circle.
  8. main Function:

    • This is the entry point of the program.
    • It generates the Ulam spiral for the range 1 to 100 using the dots representation and then creates a diagram using drawTable.
    • Finally, it exports the diagram as an SVG image using the mainWith function from the Diagrams library.

Source code in the haskell programming language

import Data.List
import Data.Numbers.Primes

ulam n representation = swirl n . map representation


swirl n = spool . take (2*(n-1)+1) . chop 1

chop n lst = let (x,(y,z)) = splitAt n <$> splitAt n lst
             in x:y:chop (n+1) z

spool = foldl (\table piece -> piece : rotate table) [[]]
  where rotate = reverse . transpose


showTable w = foldMap (putStrLn . foldMap pad)
  where pad s = take w $ s ++ repeat ' '


import Diagrams.Prelude
import Diagrams.Backend.SVG.CmdLine

drawTable tbl = foldl1 (===) $ map (foldl1 (|||)) tbl :: Diagram B

dots x = (circle 1 # if isPrime x then fc black else fc white) :: Diagram B

main = mainWith $ drawTable $ ulam 100 dots [1..]


  

You may also check:How to resolve the algorithm Comma quibbling step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the PHP programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Lua programming language
You may also check:How to resolve the algorithm MD5 step by step in the Sidef programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Rust programming language