Here we explain a “Knight jumper” method which places an element in a line, a column and a diagonal different from its starting point in a grid, to produce 4x4 magic squares in which the sums of the broken diagonals are also equal . This article deals with the history of mathematics but can be used in the classroom as a disconnected computer activity where you have to interpret an algorithm.

A digital magic square is an arrangement of numbers in a square table so that the sums in each of the horizontal and vertical rows as well as in the two diagonals are equal. Here is a magic square of order 3 with generic numbers 1 to 9 and the constant sum 15.

Magic squares of varying orders and numbers have been known for at least two millennia in various civilizations and have been important philosophical symbols and talismans in the contexts of alchemy, medicine, astronomy, and divination.

A little story

The first written certificate seems to come from China around the 1st century and represents a square of three figures per side. Over the centuries, squares have been found in the Arab, Chinese, Indian and Western worlds. Among the known examples is the inscription, accompanied by a phrase wishing victory to a prince in the famous temple of Khajuraho, which is in the current state of Madhya Pradesh in India.

Pārśvanātha temple around the 11th century.

Examples of similar use of magic squares are found in worlds that are geographically distant. In Siddha yoga of the ninth century, one of the oldest Sanskrit compendium of Ayurveda, Vṛnda prescribes eutopic tables constructed with numbers such as the sum thirty and the diagram below,

Accompanied by other rituals like the recitation of mantras, to support women in labor, is explicitly in the glosses. These squares are also found in obstetrics in Arab medical texts like Firdaus al-ḥikma by Alī al-Tabarī of the same era. In Occultâ Philosophiâ by Agrippa von Nettesheim of the sixteenth century, such a figure, inscribed in lead, represents Saturn, and has a similar use.

A non-numeric square, of Roman origin, widely used in medieval West, containing the palindrome “sator arepo tenet opera rotas” was similarly associated with childbirth.

Among the oldest textual appearances, Takao Hayashi in 1987 reconstructed a magic square of 16 cells expressing the perfume formulas of sixteen substances with their prescribed proportions in Bṛhatsaṃhitā, an encyclopedic work written by Varāhamihira in the sixth century.

Continuing with remarkable examples of Sanskrit texts, we can cite tantric manuscripts which contain such digital diagrams, such as the famous Śivatāṇḍava where we find a dialogue between Śiva and Pārvatī on the merits of the yantraṃ pañcadaśākhyam (sum diagram fifteen). Sarvavajayin man (triumph over everything).

Given their presence for such a long time, it is very likely that the methods of construction of these diagrams were known and transmitted orally long before the time of the appearance of the written traces, because a mathematical treatment of these figures does not seem to appear only around the 13th century in Sanskrit or Arabic texts and even later in Europe. According to some anthropologists, one reason for the non-diffusion of algorithms to generate such talismanic objects could come from the reluctance of ritualists.

It is interesting to note that the oriental texts do not use the expression “magic square” which is a western appellation and could come from Loubère which in the XVIIth century, in The problem of the magic squares according to the Indians says:

We call this problem the magic squares because Agrippa in his second Book of Occultâ Philosophiâ, chap. 22, teaches us that we used it as Talismans ….. having seemed quite marvelous to the ignorant, to attribute the invention to higher human minds.

Among the first known mathematical texts on the subject, one particularly notices the Gaṇitakaumudī (Enlightenment of mathematics), written by Nārāyaṇa in 1356, where the last chapter is dedicated entirely to this subject. Although the author is part of the tradition of arithmetic and pāṭīgaṇita geometry developed in particular by Śrīdhara in the 8th century, Mahāvīra and Śrīpati in the 9th century as well as Bhāskara in the 12th century, the Gaṇitakaumudī is the only text in Sanskrit to deal with the subject of magic squares as a mathematical object.

However, in another mathematical text, Gaṇitasārakaumudī, written in the early 14th century in Prâkrit, a `vernacular ‘language derived from classical Sanskrit, the author Ṭhakkura Pherū discusses these squares briefly without giving an algorithm.

Nārāyaṇa’s motivation, explained in the first two verses of the chapter, is purely mathematical and the author delves into

these good mathematics, linked to sequences, … to surprise good mathematicians, to please those who know diagrams and to chase the arrogance of bad mathematicians.

He defines technical terms and describes several methods of building Bhadra (magic square, literally “auspicious”) especially of 16 cells and curiously does not insist on the squares of 9 elements.

The knight in the 4x4 squares

Now I am developing the remarkable method of chess knight jumping to generate magic squares of order 4. The language of movements in the chessboard has also been used for the filling of Arabic magic squares (see for example Chabrāmallisī ): those of the knight, faras like turaga in Sanskrit, but also of the lady, firzān, who goes to the next square diagonally and the crazy fīl who moves two boxes diagonally. However, I do not know if the explicit construction and the counting of Nārāyaṇa are in an Arabic text.

The whole algorithm is given in one śloka [1] (stanza), and as often in the sutras (rules), condensed in verse intended to be learned by heart, the description seems enigmatic at first sight:

Put two by two the numbers obtained from a sequence, using the movements of a knight in a chess game, in descending and ascending order, and two [others] in the united cells and the cells separated by a square, [ or else] the cells must be filled by the movements of the horse on the right and on the left.

In fact, this algorithm generates all the pan diagonal magic squares of order 4, that is to say where the sums in the broken diagonals are also constant, with a sequence in arithmetic progression chosen.

Les sommes des nombres des cellules placées en horizontale, verticale et diagonale, mesurées séparément, deviennent égales.

The author does not use any particular word for “pan diagonal” for the result of his algorithm but the form karṇa-gānāṃ, genitive plural rather than gayoḥ duel, to indicate more than two diagonals.

His examples and the following list are consistent. The algorithm does not require calculation and can be applied mechanically. So, it deserves to be well decoded by contemporary comments.

First, understand that a square folds in on itself like a torus and to maintain the prescribed size of the square, the right and left edges coincide as in Figure 2 (a) as well as those of the top and bottom, Figure 2 (b). Thus, two successive horizontal movements in the same direction are equivalent, whether they are made on the left or on the right, we will note it 𝓱 = ←← = →→, similarly vertically, which we will note 𝓿=↓↓=↑↑.

A knight can move in two different ways, equivalent to a rotation of a quarter of a turn:

1- One cell vertically and two cells horizontally of which there are two possible forms; ascending A = →→ ↑ or descending D = ↓ →→ in Figure 2 (a).

2- One cell horizontally and two vertically, indicated in blue R = → ↓↓ or red L= ↓ ← ↓, for rectus and laevus, in Figure 2 (b).

Thanks to self-folding, the direction of repeated movements is not important. Note that a pan diagonal magic square is constructed from a single type of jump A, D, L or R: once started, the movements always progress in the same direction for a given quarter.

The decrypted algorithm

We use the generic sequence {1,2, …, 16} (It is also possible to choose another arithmetic progression instead of this generic case) containing 4 pada (quarter) {1, …, 4} until {13, …, 16} to describe the filling. The movements for {1,2,3,4} being chosen, the three others will then follow, on the same principle, starting in a box avoiding the previous ones: 1, 5, 9, 13 will neither be in the same lines nor in the same columns.

Let’s see for the first quarter, according to the algorithm of Nārāyaṇa, there will, therefore, be a jump between 1 and 2 then between 3 and 4. The movement between 2 and 3 will be of another type, let’s call it “non-knight”. It will be done in the same direction and, either in the cells united by the diagonal or in the cells distant by a place (that is to say a movement 𝓱 or 𝓿).

For non-knight movements on the diagonal, let us note by α, δ, ρ, λ the directions respectively ascending, descending, right and left that we choose. There is no need to specify the other direction. For example, right or left for the ancestry will already be fixed by the jump of the knight R or L.

These movements are used to put the elements of a quarter of a sequence in different rows and columns, increasing each time by one in the direction of progression. The third and fourth quarters will then return in the opposite direction to balance this sum.

Note that each filling contains two-quarters of movements in the same direction and two of its opposite (for example two ascending and two descending), the variations being in non-knight movements. This is true even for non-knight movements 𝓱 or 𝓿: it suffices to consider quarters:
{1,3,5,7}, {2,4,6,8}, {9,11,13,15} and {10,12,14,16}.

Let us now analyze how the magic square of Khajuraho is constructed by the method of Nārāyaṇa.

The magic square of Khajuraho

In this example, we arbitrarily fix the starting cell at row 1 and column 3 and choose D, the descending direction as the first movement of the knight. According to the algorithm, all movements during a stage will follow the same direction as his first movement of the knight, here, descending.

The step of knight D starting from the cell of the number 1, thus defines the cell where to put the number 2.

The displacement between 2 and 3 must also be descending and there will be three valid choices, of a cell by the two descending diagonals, λ, and ρ, or vertically by two boxes, 𝓿. We choose the right diagonal ρ.

Then the knight, still descending, must start from 3 to reach 4 without any choice, it is a D movement.

The first step is therefore described by DρD.

The second quarter will start in a cell adjacent to the last cell in the first quarter. Thus, for the number 5, there are two possibilities: ascending = a or right = r, so as not to put it in the same row or the same column as the number 1.

The purpose of this step offset is the constant sum of the rows and columns. In the construction of the magic square of Khajuraho, we choose the ascending march for 5.

The coding, therefore, follows the process in reverse, ie AρA for the third quarter, d for the non-knight movement, and then AλA.

The constraint on the complement at 17 results in the square obtained being pandiagonal. We easily check the sums in broken diagonals:
(16 + 6) + (1 + 11) = (12 + 8 + 5) +9 = … = 34.

Another example: the square of al-Būnī

The famous Arab pan diagonal magic square of al-Būnī and others of the 13th century,

is constructed by placing 1 in the upper right corner and following the path DλD for the first quarter, to place the 5. Consequently, the three remaining steps are AλA, DρD, r AρA. This square can be obtained by a 90-degree rotation of those reconstructed by Hayashi, generated by the formula starting with LαLa with the lower right corner as the starting cell.

Enumeration: How many are there?

Furthermore, Nārāyaṇa evokes an arithmetic question which has not been found in any other period text: How many pan diagonal magic squares of order 4, filled with the numbers 1 to 16, can be obtained? This question requires you to fully understand all the choices made during the execution of the algorithm.

With the place of the number 1 and the step of the knight fixed to start, there are three choices to decide the non-knight movement and finally two for the transition. So corresponding to each step of the knight there are six possibilities of filling the square. Let’s start, for example, with the downward movement of the knight, of which we have seen the examples.

Figure 3: First downward knight movement

But this is one of the four starting step choices and we come to 24 possibilities for a given starting place!

In Gaṇitakaumudī, this final result is cited as a rule and the 24 possibilities containing the integers 1 to 16 with the number 1 in the cell in the upper left corner are then explicitly illustrated.

The 24 possibilities, Manuscript 104595, Sarasvatī Bhavan, Vārāṇasi

Finally noting that any cell can be used as a starting box, the total number of pan diagonal magic squares with integers from 1 to 16 becomes 24 × 16 = 384, which is precisely found in the text catur-aśīty- Adhika-śata-traya.

Combining magic squares of order greater than 4 becomes much more difficult. The number of pan diagonal magic squares of order 5 is 28800 and is still not known for orders higher than 6. The knight’s march does not generate all the pan diagonal magic squares of a higher order.

Here you will find proof that the Nārāyaṇa algorithm works, that it gives pan diagonal magic squares and all pan diagonal magic squares.


[1] GK.14.10.11-a caturaṅgaturagagatyā dvau dvau śreḍhīsamudbhavāv aṅkau/
nyasya kramotkrameṇa ca koṣṭhaikyaikāntareṇa ca
tau/ savyāsavyaturaṅgamarītyā koṣṭhān prapūrayed aṅkaiḥ/



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store



Please follow me since now we need 100 min follower on medium