Label the squares A to I:
A B C
D E F
G H I
If each row and column is to sum to 40 then the three rows added together must sum to 120:
(A+B+C)+(D+E+F)+(G+H+I)=40+40+40=120, which is the sum of all numbers used.
The consecutive numbers 1 to 9 add up to 45, which is 75 short of 120. If we use the numbers 2, 4, 6, ..., 18 the sum would be 90, 30 short of 120. If we bias the numbers by starting from a different number, we can make up the difference.
Let the numbers be in order, nx+a where x is between 1 and 9, and a is a bias (a number to be added to each nx) and n is to be an integer, then the sum of all the numbers is n+a+2n+a+3n+a+...+9n+a=45n+9a=120. That is, 5n+a=120/9=40/3. So one solution is n=2 and a=40/3-10=10/3.
For numbers 1 to 9, a magic square is typically:
8 1 6
3 5 7
4 9 2
We use the conversion nx+a: 1→16/3, 2→22/3, 3→28/3, 4→34/3, 5→40/3, 6→46/3, 7→52/3, 8→58/3, 9→64/3:
58/3 16/3 46/3
28/3 40/3 52/3
34/3 64/3 22/3
In this square the fractions sum to 40.
If we write a=40/3-5n=5(8-3n)/3 or n=(40/3-a)/5=(40-3a)/15 or 8/3-a/5, we can find other values for a and n. Then we can convert the numbers 1 to 9 using the formula x→nx+a.
GENERAL 3 x 3 MAGIC SQUARE SOLUTIONS
Represent square using letters:
A B C
D E F
G H I
A+B+C=S=D+E+F=G+H+I; A+B+C+D+E+F+G+H+I=3S, where S is the common sum.
A+E+I=B+E+H=C+E+G=D+E+F=S
(A+B+C+D+E+F+G+H+I)+3E=4S; 3S+3E=4S, E=S/3.
A+E+I=S, I=S-E-A, I=2S/3-A.
H=S-E-B, H=2S/3-B.
C=S-(A+B).
G=2S/3-C=2S/3-S+(A+B), G=A+B-S/3.
D+G=B+C=B+S-(A+B)=S-A; D=S-A-G=S-A-A-B+S/3, D=4S/3-(2A+B).
F=2S/3-D=2S/3-4S/3+2A+B, F=2A+B-2S/3.
Completed square:
A B S-(A+B)
4S/3-(2A+B) S/3 2A+B-2S/3
A+B-S/3 2S/3-B 2S/3-A
So A and B are arbitrary; S must be a multiple of 3 if square is to be whole numbers only.
EXAMPLE: A=1, B=5, S=18:
1 5 12
17 6 -5
0 7 11
Since squares can be rotated and reflected, A and B could have been 1 and 17, 12 and 5, 11 and -5, etc., to produce the same square effectively.