📜 ⬆️ ⬇️

Magic constant

There are some squares that are called magical. Well, probably everyone knows that the sum of numbers in such squares along horizontals, verticals and main diagonals is the same, that is, equal to the same number, this number-sum is called the magic constant (hereinafter M n , where n is the size of the square; n> 2). Back in school, I remember the formula for calculating this constant: M n = n * (n 2 + 1) / 2, it was not clear for me where it came from ... here we will try to derive it, maybe someone has already deduced it, perhaps also Perhaps in a different way, it doesn't matter just writing.

Writing again the numbers in squares, once noticed such a thing. If you enter numbers from 1 to n 2 in columns from left to right, you always get a magic constant when adding numbers along any main diagonal, here you can see it:

M 3 :
1 4 7
2 5 8
3 6 9

M 4 :
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
')
According to the formula:

M 3 = n * (n 2 + 1) / 2 = 3 * (3 * 3 + 1) / 2 = 30/2 = 15
M 4 = n * (n 2 + 1) / 2 = 4 * (4 * 4 + 1) / 2 = 68/2 = 34

Diagonally (bold above):

M 3 = 1 + 5 + 9 = 15
M 4 = 1 + 6 + 11 + 16 = 34

Unlike formulas, diagonals are able to give an answer what happens. Consider the numbers on the diagonals:

M 3 = 1 + 5 + 9
M 4 = 1 + 6 + 11 + 16

Rewrite differently:

M 3 = 1 + (3 + 2) + (3 * 2 + 3)
M 4 = 1 + (4 + 2) + (4 * 2 + 3) + (4 * 3 + 4)

Did you notice? Now in general form from n:

M n = 1 + (n + 2) + (n * 2 + 3) + (n * 3 + 4) + (n * 4 + 5) + ... + (n * (n-1) + n)

Rearrange this (bold)
M n = 1 + (n + 2 ) + (n * 2 + 3 ) + (n * 3 + 4 ) + (n * 4 + 5 ) + ... + (n * (n-1) + n )

and this (bold)
M n = 1 + ( n + 2) + ( n * 2 + 3) + ( n * 3 + 4) + ( n * 4 + 5) + ... + ( n * (n-1) + n)

and get:

M n = (1 + 2 + 3 + 4 + 5 + ... + n) + (n + n * 2 + n * 3 + n * 4 + ... + n * (n-1))

put n out of the bracket:

M n = (1 + 2 + 3 + 4 + 5 + ... + n) + n * (1 + 2 + 3 + 4 + ... + (n-1)) [1]

Now we introduce a new designation

S n = 1 + 2 + 3 + ... + n [2]
then
S n-1 = 1 + 2 + 3 + ... + (n-1) = S n - n [3]

Now we rewrite the formula [1] with the notation [2] and [3], and we get:

M n = S n + n * (S n - n) [4]

or so:

M n = S n * (n + 1) - n 2

[five]

S n with this in mind -



obviously calculated by the formula S n = n 2/2 + n / 2 = n * (n + 1) / 2,
Substitute in [5]:

M n = S n * (n + 1) - n 2 = n * (n + 1) * (n + 1) / 2 - n 2 = n * (n 2 + 2 * n + 1 - 2 * n) / 2 = n * (n 2 + 1) / 2

M n = n * (n 2 + 1) / 2

CTD

Source: https://habr.com/ru/post/418725/


All Articles