Computer Algorithms for Staircase and Uniform Step Squares (Part IV)

A stair

This section is a continuation of Part III where it was shown which Staircase and Uniform Step squares were magic and which were not. It was also shown how to create a table consisting of the following four symbols (in Soduko fashion):

Ö   magic diabolic
O  magic (not diabolic)
X  constructible (not magic)
 Inconstructible (not magic)

in which one of these symbols is placed into the same cell of a square where the initial number 1 is to be placed. A diabolic square, labeled here as an O with horns (el Diablo), is a magic square in which every row, column, two major diagonals and its negative diagonals (all the diagonals going from the left to the right) sum to the magic number. Construction of a Staircase square where the square is the Loubère type is normally done manually but computer methods based on a number of various computer languages are available in Rosettacode.

The Rosettacode section on VBScript contains a code which is readily amenable to JavaScript programming and I found the do while code to be the hallmark of the program. This code was borrowed and modified for the programs employed here. It would be nice to know which individual(s) coded this so I can give them the credit that is due. VBScript appears to have been written anonymously, so whoever you are, my email is at the end of this page.

In addition, two Javascript algorithms were coded for the Uniform Step Squares (which again normally require construction by hand) where the knight moves I chose were 1 column right, 2 rows up, K(1,2), and 2 columns right, 2 rows up, K(2,2). However, since Part V follows Parts I-IV (a general method for the construction of Staircase and Uniform Step squares) the computer programs were coded so as to include odd squares where the initial number 1 can be placed in any cell of the square being constructed.

Javascript Coding of Staircase and Uniform Step Squares

Each of the individual cells can be accessed by use of the following coordinate system which is readily convertible into computer code. First the rows and columns in the original square are coded from 0..n, thus giving each cell a unique coordinate (r,c). For example, in table Square 7 the first cell at top left is (0,0) and the last bottom right is (n-1,n-1), both in light yellow. In addition, the second row and next to last column contain two other numbers j/k (what I called the Knight shifts) that correspond to the break moves at the end of a diagonal, which are also required and were discussed in Part III. For example using n = 7, the 7th order square is set up in the coordinate system as follows with j = c1 and k = r1 (the actual variables used in the programs):

Square 7
0123456c/r
1350246j/k
10
31
52
03
24
45
66

When we follow this approach and use 7th order squares the following three squaretype tables below are obtained. By squaretypes I mean: for every nth order, a square is constructed (where the initial 1 is placed into every cell of a square) and the results from each square placed into these squaretype tables. The first two squaretypes below have already been discussed in Part III but have been placed here so as to compare with the third squaretype. Inputting the requisite data into either of the Javascripts programs, outputs the appropriate square and its magic properties. Once we know this information, the appropriate symbol is placed into the required cell of the squaretype table.

For example, if the initial values to be used is placed into (row 0,column 1) of a square to be constructed, then once we know the square's type, we place a symbol for that type into (row 0,column 1) of the squaretype table. Easy peasy. Every square within each of the three squaretype tables was run by its appropriate Javascript program. The first two squaretype tables that had originally been constructed by hand were also run in the new Javascript programs and the Javascript results agreed with the original squaretypes. Now large squares of the Staircase and Uniform Step types, mentioned here, as well as really large order squares, which were a chore to produce, can be constructed in a blink of an eye.

Staircase 7
X O Ö ÖÖÖ
X Ö O Ö Ö Ö
X Ö Ö O Ö Ö
XÖÖ O ÖÖ
X Ö Ö ÖO Ö
X Ö Ö ÖÖ O
X XX X XX
Uniform Step 7 (1,2)
X Ö ÖÖO Ö
X Ö Ö Ö Ö O
X X X XX X
XOÖ Ö ÖÖ
X Ö O Ö ÖÖ
X Ö O ÖÖ Ö
X X ÖÖ O Ö
Uniform Step 7 (2,2)
Ö Ö O ÖXÖ
Ö Ö Ö O XÖ
X XX X X X
ÖÖÖ X OÖ
Ö Ö Ö XÖO
O Ö Ö XÖ Ö
O ÖÖ X Ö O

The Multiple Square at a Time Option

It can be seen that the rightmost squaretype table 3 appears to be a hybrid of both squaretype tables 1 and 2. In fact 3 behaves just like 1 in that squares below the diagonal are identical to those of the upper half. Rotation of both 1 and 3 by 180o degrees along the diagonal show that the bottom and top are superimposable. This can be seen by running:

JS Program 5x5   (a copy of the text file is available on 5x5 text)

which constructs and prints out the 25 squares in a 5×5 table format and which upon inspection shows the top and bottom halves of the 5×5 table of squares to be identical upon rotation along the main diagonal. One may also input larger orders of n into this program. However, n = 7 already prints out 49 squares which can be read from the screen by scrolling up and down. From the 5×5 square output we can determine if we have magic (diabolic or non diabolic), non magic squares and nonconstructible squares with the break move at the top of the square. Since this progam outputs a lot to the screen it will only be used for certain purposes such as the 5x5. Larger squares will be output one square at a time and will be discussed in the section below: The One Square at a Time Option.

Since depicting squares with n > 7, already outputs a large number of squares, a larger n might be too much for one to handle. We can prevent this sensory overload, by changing the values of s and jcount, in the program, to lower values, for example, for an n = 9 with (3 rows x 2 columns) the following s < 3 and jcount < 2 generates 6 squares of three types, since diabolic squares are not possible with n odd orders that are divisible by 3:

JS Program 3x2.

The two captions at the bottom of each square shows the sums of the first row and columns. I have found that the only squares that are magic are those where the sums of row 1 and col 1 are in this case the magic sum S = 369. Those that are scattered with zeros are inconstructible.

As a small digression and to make a point, I said that the squaretype table 3 appears to be a hybrid of squaretype tables 1 and 2. Let's look at the n = 5 order as shown below. The squares within this squaretype table each also form hybrid squares as shown, for example in the partial squares Kn(1,2), where (1,2) is the break move at the end of each diagonal. From the squaretype table coordinate (3,0) it can be seen that this square is magic but not diabolic. If we start to fill the first diagonal of a square using these coordinates we can fill up the diagonal in "leapfrog" fashion where first one cell on the diagonal is filled followed by leapfrogging over the next cell to a third cell. As can be seen in I and II the square can be filled either by Staircase or Uniform Step rules. It's like looking at a 3D representation of a cube on paper, where the cube can flip back and forth between two configurations depending on how one looks at the cube.

Stair/Uniform 5
ÖÖO X
X X X X
ÖÖ X O
O Ö XÖ
O Ö X Ö
K(1,2) I
2
1 3
K(1,2) II
5
2
4
1 3

The One Square at a Time Option

1. Types of Magic/Non-Magic Squares

A third option is to use a program that produces just one square at a time when we input the requisite n, row and column values. One can sit at the computer and input the three numbers for each run, n, r and c (the two coordinates), sit back, while the program rapidly computes the requisite R, C, c1 and r1 and stores each into arrays. For example, when n = 7 the values stored in R, C, c1 and r1 are listed in:

JS Program R,C,c,r values

Although the inputs r and c are equivalent to R and C, the program calculates and stores these values as a group. All four values are available in storage, as a group, for running calculations and may be outputted, if desired.

So to continue, the inputted coordinates accesses the appropriate (R,C,c1,r1), then constructs and outputs the square. Since only one square is being outputted at a time the Sums of two left diagonals appear below the square so as to indicate whether the square is diabolic or not.

I have found that the two diagonals, one each to the right and left of the main left diagonal (from top leftmost cell to bottom rightmost cell) are all that is needed for determining the "diabolicity" of a square. In a sense this is good since we don't need to calculate all the left diagonals, which equal n-1, for every square, thus reducing the amount of coding. Moreover, every time n is increased more equations would have to be encoded to keep the number of left diagonal equations at n-1.

There are four basic types of squares listed in the table below:

Four Types of Squares
Diabolic (Ö)Non-Diabolic (O)Non-Magic (X)Inconstructible ()
Row 1 = Col 1 =SRow 1 = Col 1 =S Row 1 = Col 1 ≠S Row 1 ≠ Col 1 ≠S
SumD1 = SumD2 =SSumD1 ≠ SumD2 SumD1 = SumD2 SumD1 ≠ SumD2 ≠S
**** SumD1 ≠ SumD2 **

A square will be depicted on screen along with their magic sum (S) according to whether row 1 equals column 2 and whether the sum of its two left diagonals (SumD1 or SumD2) are equal or not. Non-magic squares are the odd man out giving two results which don't really matter.

2. The Staircase and Uniform Squares Coding

This section lists the programs that are used to generate the general Staircase K(1,1), the Uniform Step K(1,2) and the Uniform Step K(2,2) Squares, where K(r,c) signifies how the numbers are to be placed the square: a row step of r followed by a column step of c. All three programs use similar coding, the difference being in the while loop which decides into which cells the consecutive numbers are to be written (see the text files for the differences). The program's output is just one square at a time including whether a square is diabolic or not, something which was not included in the 5×5 program above. These three JS programs are listed for n = 11 using the same values of r = 9,c = 2. It will be seen that all three squares are diabolic.

(a) Staircase 11 and text file Staircase 11 text
(b) Uniform 11 K(1,2) and text file Uniform 11 K(1,2) text
(c) Uniform 11 K(2,2) and text file Uniform 11 K(2,2) text

The following three JS programs are compared at r = 9 and c = 0:

(a) Staircase 11
(b) Uniform 11 K(1,2)
(c) Uniform 11 K(2,2)

The results this time are that (a) is non magic, (b) is non magic and (c) is magic but not diabolic. Although (c) is a hybrid of both (a) and (b), the non magic type of the formers does not ensure the same result for the latter.

Note the test files can be converted into htm files if desired. Remember to input just the three values: n, r and c. The latter two require the square's coordinates as explained above.

To go back to Part III Con't. To go to Part V which discusses 15th and 45th order squares. To return to homepage.


2021 by Eddie N Gutierrez. E-Mail: enaguti1949@gmail.com