Showing posts with label knight tour. Show all posts
Showing posts with label knight tour. Show all posts

Monday, 9 January 2023

The Trapped Knight

THE TRAPPED KNIGHT: PART ONE (A REVIEW)


I've already written about a trapped knight in a post from May 23rd 2020 titled Knight Tours in which the Knight gets trapped on an infinite chess board if it follows these rules:

Suppose we devise a Knight Tour such that the Knight starts on square 0 and then moves always to the unvisited square closest to the origin. "Closest to the origin" is meant in the sense of Euclidean distance, and in case of a tie, the square coming earliest on the spiral is chosen.

On the first move, the Knight could move to squares 9, 11, 13, 15, 17, 19, 21 or 23. All these squares are the same distance from the 0 square but 9 is chosen because it comes first in the spiral. From 9, subsequent squares are 2, 5, 8, 3, 6, 1, 4, 7, 10, 13 and so on. 

 Figure 1 depicts the situation:


Figure 1
As I wrote in my post at the time:

The initial terms are of the sequence are:

0, 9, 2, 5, 8, 3, 6, 1, 4, 7, 10, 13, 28, 31, 14, 11, 26, 23, 44, 19, 22, 43, 40, 17, 34, 37, 18, 15, 32, 29, 52, 25, 46, 21, 76, 47, 50, 27, 12, 33, 16, 39, 20, 45, 24, 51, 48, 77, 114, 73, 70, 105, 38, 35, 60, 93, 30, 53, 84, 49, 78, 115, 74, 41, 68, 103, 36, 61, 94, 57, 54, 85, 124, 81, ...

What's really interesting about this tour is that the knight gets trapped at the 22325th move, where it can't reach any unvisited square. The square on which the Knight is stuck is 25983. 

Of course, if the numbering began from one instead of zero, then the square on which the knight becomes stuck would be 25984. 

THE TRAPPED KNIGHT: PART TWO

Yesterday I turned 26943 days old and this number is also associated with a trapped knight that gets trapped following a different set of rules. To begin with, the infinite chessboard is numbered slightly differently. See Figure 2.


Figure 2: screenshot from Numberphile video

As can be seen, the numbering starts from one, not zero. The rule for the knight's movements is that it must move to the lowest available square and not the square that is closest to the origin. The numbers of the squares that the knight moves to are listed in OEIS A316667: 

 
A316667

Squares visited by a knight moving on a spirally numbered board always to the lowest available unvisited square.

The initial terms are:

1, 10, 3, 6, 9, 4, 7, 2, 5, 8, 11, 14, 29, 32, 15, 12, 27, 24, 45, 20, 23, 44, 41, 18, 35, 38, 19, 16, 33, 30, 53, 26, 47, 22, 43, 70, 21, 40, 17, 34, 13, 28, 25, 46, 75, 42, 69, 104, 37, 62, 95, 58, 55, 86, 51, 48, 77, 114, 73, 108, 151, 68, 103, 64, 67, 36

As the OEIS comments state: 

This sequence is finite: At step 2016, square 2084 is visited, after which there are no unvisited squares within one knight move.

The number of steps required to become trapped, 2016,  is far less than the number required in the PART 1 example where 22325 steps were required. Figure 3 shows the path of the knight as it starts at 1 and ends up at 2084. The path was generated by Python code that I found on this site. I pasted the code into SageMathCell and it worked just fine. Here is the permalink. This graphic always appears in the Numberphile video.

Figure 3

If 2084 is the end of the knight's tour and  26943 is never visited, then how can this number  be involved? The answer is that we can continue the tour if we repeatedly block the squares where the knight would not have any available moves. Thus when we get to 2467, the square before 2084, we block the latter and move instead to the next lowest number and the tour continues. See Figure 4:


Figure 4: screenshot from Numberphile video

Eventually the knight gets trapped again once it moves to square 2720. Again we block that square and allow the knight to move to the next lowest square. These blocked squares constitute OEIS A323714:

 
A323714

Squares where knight moving to a lowest unvisited square on a spirally numbered board will have no available moves.



The initial members of the sequence are:

2084, 2720, 3325, 3753, 7776, 5632, 7411, 8562, 14076, 8469, 9231, 22702, 14661, 21710, 21078, 25809, 27112, 24708, 19844, 26943, 26737, 32449, 31366, 45036, 37853, 37188, 43318, 62095, 67401, 68736

It's more useful to put these terms in ascending order, in which case the sequence appears as:

2084, 2720, 3325, 3753, 5632, 7411, 7776, 8469, 8562, 9231, 14076, 14661, 19844, 21078, 21710, 22702, 24708, 25809, 26737, 26943, 27112, 31366, 32449, 37188, 37853, 43318, 45036, 62095, 67401, 68736

Now we can see that the next number in the sequence is 27112 and this occurs on June 26th 2023. After this, there is a considerable gap before 31366 is reached.

Monday, 17 May 2021

Prime Semi-Magic Squares

 I've posted about magic squares (and cubes) before, specifically:

Yesterday, I turned 26339 days old and one of the properties of this prime number is that it's a member of OEIS A270865:


 A270865

Smallest primes of 4 X 4 semi-magic squares formed from consecutive primes. 
 

A semi-magic square has all its rows and columns adding to a constant number, but not its diagonals. Up to 26339, the OEIS sequence runs:
5, 19, 29, 31, 37, 47, 53, 79, 397, 409, 599, 787, 1229, 1381, 1439, 1993, 2087, 2767, 4003, 4159, 4931, 5791, 5981, 8117, 9293, 9349, 9833, 10939, 10979, 11213, 12553, 12907, 14557, 16361, 18047, 21089, 21557, 21577, 25903, 26339

In the OEIS comments for the sequence, examples are shown for 5 and 19:$$\begin{array}{|c|c|c|c|}

\hline 5 & 7 & 53 & 59 \\

\hline 29 & 61 & 23 & 11\\

\hline 43 & 37 & 31 & 13\\

\hline 47 & 19 & 17 & 41 \\

\hline

\end{array}$$ $$\begin{array}{|c|c|c|c|}

\hline 19 & 23 & 79 & 83 \\

\hline 53 & 67 & 37 & 47\\

\hline 61 & 41 & 59 & 43 \\

\hline 71 & 73 & 29 & 31 \\

\hline


\end{array}$$In the magic square beginning with 5, the 16 primes (5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61) total 496 and every row and column totals 496 ÷ 4 = 124 which is the magic constant of the square. The sum total of the primes must always be a multiple of 4. We know the next set of primes starts with 19 so what happens if we use a set of primes starting with 7, 11, 13 and 17:

  • 7 gives a total of 558 which is not divisible by 4
  • 11 gives a total of 662 which is not divisible by 4
  • 13 gives a total of 684 which is divisible by 4 to give 171
  • 17 gives a total of 750 which is not divisible by 4
From this we can see that divisibility of the prime sum by 4 is a necessary but not sufficient condition for ensuring that a semi-magic square can be created. 13 serves as a reminder of this. A little thought reveals that for an \(n \times n \) magic square, not only must the sum of the terms be divisible by \(n\), the quotient must be even when \(n\) is even and odd when \(n\) is odd. As can be seen, if we start with 13 as the first consecutive prime, the quotient is odd. The reason is that in the case of a 4 x 4 magic square, each column and row consists of four odd primes that when added together must produce an even total.

In the case of 26339, the primes are:

26339, 26347, 26357, 26371, 26387, 26393, 26399, 26407, 
26417, 26423, 26431, 26437, 26449, 26459, 26479, 26489

These primes total 422584 and thus the magic constant is 105646. The problem is how to arrange these primes into a semi-magic square. As can be seen, the pattern of the placement of primes is different between the semi-magic square beginning with 5 and the one beginning with 19. It seems as if each arrangement of numbers might be unique. I tried using the same pattern as found in Durer's famous 4 x 4 magic square but no luck. There are a staggering 20,922,789,888,000 or over twenty trillion ways to form a 4 x 4 grid of 16 numbers  and even SageMathCell cannot search through all these possibilities. What I'm looking for is a method to populate a 4 x 4 semi-magic square with 16 non-consecutive elements.

The number of groups of primes making up the 4 x 4 semi-magic squares are relatively rare. 26339 marks the first member of the 40th such group. Not surprisingly, the number of groups of primes making up 4 x 4 fully magic squares are ever rarer. The following sequence begins 31, 37, 1229, 4931, 12553, 3259909, 3324329, 9291521, ...


 A260673

Smallest primes of 4 X 4 magic squares formed from consecutive primes.   


Here is the magic square of consecutive primes beginning with 37. It has a magic constant of 258:$$\begin{array}{|c|c|c|c|}\hline 37 & 83 & 97 & 41 \\
\hline 53 & 61 & 71 & 73\\

\hline 89 & 67 & 59 & 43\\

\hline 79 & 47 & 31 & 101 \\

\hline

\end{array}$$This of course doesn't solve my problem of how to arrange the primes in my 4 x 4 semi-magic square with its magic constant of 105646. One thing to consider would be the gaps between the primes. These average of these gaps is 10 and the actual gaps are:

26339 to 26347 is a gap of 08 with total of 008
26347 to 26357 is a gap of 10 with total of 018
26357 to 26371 is a gap of 14 with total of 032
26371 to 26387 is a gap of 16 with total of 048
26387 to 26393 is a gap of 06 with total of 054
26393 to 26399 is a gap of 06 with total of 060
26399 to 26407 is a gap of 08 with total of 068
26407 to 26417 is a gap of 10 with total of 078
26417 to 26423 is a gap of 06 with total of 084
26423 to 26431 is a gap of 08 with total of 092
26431 to 26437 is a gap of 06 with total of 098
26437 to 26449 is a gap of 12 with total of 110
26449 to 26459 is a gap of 10 with total of 120
26459 to 26479 is a gap of 20 with total of 140
26479 to 26489 is a gap of 10 with total of 150

The reason I've listed these gaps between the primes is that a lot of importance seems placed on numbers been in arithmetic progression. At this location on Quora, there is a method shown for filling the traditional 4 x 4 magic square with the numbers from 1 to 16. See Figure 1.

Figure 1: source

I tried this using my prime numbers with 26339 replacing 1, 26347 replacing 2 etc. but it didn't work. So my search continues but along the way I've discovered new types of magic squares. For example, bimagic means a magic square remaining magic after each of its numbers have been squared. The smallest possible 4×4 semi-bimagic square of prime numbers begins with 29 and is shown in Figure 2:
Figure 2: source

For this magic square, the magic constant is 1190 and when the terms are squared, the magic constant becomes 549100. Lots of interesting information on magic squares to be found at the source site listed in Figure 2.

The knight's tour often comes up in the construction of magic squares as can be seen in this initial populating of the squares in Figure 3. A knight's tour is only possible on a board with an even number of squares. However, it is apparently not possible on a 4 x 4 board. In fact, the 5 x 6 and 3 x 10 boards are the smallest rectangular boards that have knight's tours. Source.

Figure 3: source

From the same source as listed in Figure 3, the method shown in Figure 4 is suggested as a way to arrange 16 elements into a 4 x 4 magic square:

Figure 4: source

I'm wondering if this strategy might be successful if used with the gaps that I've listed between the primes. I don't have four of every \(a,b,x,y\) but my magic square only needs to be semi-magic. In Figure 2, the knight moves can be seen in the path between \(a\) and \(a+x\), \(b\) and \(b+x\) etc. So far though no luck. I'll keep trying and add to this post when I succeed.

It can be noted that 26339, the first prime in the set of 16 primes that I'm trying to arrange into a semi-magic square, is a Luhn prime of the first order because 26339 + 93362 = 119701 which is a prime number. This is the property that defines a Luhn prime, namely that the prime number itself when added to its reverse, produces a new prime. I posted about Luhn Primes in a post on my birthday, April 3rd 2021. However, this property of 26339 doesn't seem to help in solving my problem.

One approach is to subtract 26339 from every term so that the sequence starts with zero. This gives: 0, 8, 18, 32, 48, 54, 60, 68, 78, 84, 92, 98, 110, 120, 140, 150 and these terms have a median of 73. If we subtract 73 from each we get:

-73, -65, -55, -41, -25, -19, -13, -5, 5, 11, 19, 25, 37, 47, 67, 77

The magic constant for this set of numbers is -2. If I subtract 72, the magic constant is 2, so a magic constant of zero isn't possible. Perhaps it will be easier to work with these smaller numbers. If I come up with a semi-magic square for these numbers, then I simply need to add 26339 + 73 = 21412 to the numbers above.