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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "/dynamic" for Enum
import "/math" for Int
import "/str" for Char
import "/fmt" for Fmt

var Direction = Enum.create("Direction", ["right", "up", "left", "down"])

class Ulam {
    static generate(n, i, c) {
        if (n <= 1) Fiber.abort ("'n' must be more than 1.")
        var s = List.filled(n, null)
        for (i in 0...n) s[i] = List.filled(n, "")
        var dir = Direction.right
        var y = (n/2).floor
        var x = (n % 2 == 0) ? y - 1 : y  // shift left for even n's
        for (j in i..n * n - 1 + i) {
            s[y][x] = Int.isPrime(j) ? (Char.isDigit(c) ? Fmt.d(4, j) : "  %(c) ") : " ---"
            if (dir == Direction.right) {
                if (x <= n - 1 && s[y - 1][x] == "" && j > i) dir = Direction.up
            } else if (dir == Direction.up) {
                if (s[y][x - 1] == "") dir = Direction.left
            } else if (dir == Direction.left) {
                if (x == 0 || s[y + 1][x] == "") dir = Direction.down
            } else if (dir == Direction.down) {
                if (s[y][x + 1] == "") dir = Direction.right
            }

            if (dir == Direction.right) {
                x = x + 1
            } else if (dir == Direction.up) {
                y = y - 1
            } else if (dir == Direction.left) {
                x = x - 1
            } else if (dir == Direction.down) {
                y = y + 1
            }
        }

        for (row in s) Fmt.print("$s", row)
        System.print()
    }
}

Ulam.generate(9, 1, "0") // with digits
Ulam.generate(9, 1, "*") // with *

  

You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Ada programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the Elm programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Ya programming language
You may also check:How to resolve the algorithm Superpermutation minimisation step by step in the Julia programming language
You may also check:How to resolve the algorithm A+B step by step in the NS-HUBASIC programming language