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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ulam spiral (for primes) step by step in the 11l 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 11l programming language

Source code in the 11l programming language

F cell(n, =x, =y, start = 1)
   V d = 0
   y = y - n I/ 2
   x = x - (n - 1) I/ 2
   V l = 2 * max(abs(x), abs(y))
   d = I y >= x {(l * 3 + x + y)} E (l - x - y)
   R (l - 1) ^ 2 + d + start - 1

F show_spiral(n, symbol = ‘# ’, start = 1, =space = ‘’)
   V top = start + n * n + 1
   V is_prime = [0B, 0B, 1B] [+] [1B, 0B] * (top I/ 2)
   L(x) 3 .< 1 + Int(sqrt(top))
      I !is_prime[x]
         L.continue
      L(i) (x * x .< top).step(x * 2)
         is_prime[i] = 0B

   (Int -> String) f = _ -> @symbol

   I space == ‘’
      space = ‘ ’ * symbol.len

   I symbol.empty
      V max_str = String(n * n + start - 1).len
      I space == ‘’
         space = (‘.’ * max_str)‘ ’
      f = x -> String(x).rjust(@max_str)‘ ’

   V cell_str = x -> I @is_prime[x] {@f(x)} E @space

   L(y) 0 .< n
      print((0 .< n).map(x -> cell(@n, x, @y, @start)).map(v -> @cell_str(v)).join(‘’))
   print()

show_spiral(10, symbol' ‘# ’, space' ‘  ’)
show_spiral(9, symbol' ‘’, space' ‘ - ’)

  

You may also check:How to resolve the algorithm Sierpinski triangle step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Round-robin tournament schedule step by step in the BASIC programming language
You may also check:How to resolve the algorithm Kronecker product based fractals step by step in the Haskell programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Sidef programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Oz programming language