Magic Squares & Magic Cubes for the HP-41
Overview
1°) Magic Squares
a) Magic & Panmagic Squares
b) More Panmagic-Squares ( prime
order > 4 )
c) Panmagic Squares of order n =
4m
d) Magic Squares of order n = 4m
e) Magic Squares of order n = 4m+2
f) Bimagic Squares
2°) Magic Cubes
a) Magic & Panmagic Cubes
b) More Panmagic-Cubes ( prime order
> 10 )
c) Magic Cubes of order n = 4 m
d) Magic Cubes of order n = 6 , 10
, 14 , ....
3°) Magic Squares and Cubes
4°) A few Tests
a) Magic Square ?
b) Panmagic Square ?
c) Magic Cube ?
5°) 4D Magic HyperCubes
-A few programs are listed in "Creating a Matrix" & "Creating a Hypermatrix"
to make magic squares and magic cubes.
-They have been re-written to save bytes and to deal with larger order:
they work provided the calculations do not exceed 10^10.
-Several routines have also been added to create more magic arrays.
1°) Magic Squares
a) Magic & Panmagic
Squares
-The elements of a normal magic square are 1 , 2 , .............
, n2 and it has the following property:
All its row sums, column sums and both diagonal sums are equal to
the magic constant M = n.(n2 + 1)/2
-A panmagic square ( also called pandiagonal square ) is a magic square
where all the broken diagonal sums are also equal to the magic
constant M = n.(n2 + 1)/2
Formulae:
-The element ai,j of a magic square of odd order n is calculated by ai,j = 1 + ( i + 2 j - 2 ) mod n + n [ ( i + j - 1 + Int(n/2) ) mod n ]
-The element ai,j of a panmagic square of order n where n is neither a mutiple of 2 nor a multiple of 3 is calculated by
ai,j = 1 + [ ( i - 1 ) + 3 ( j - 1 ) ] mod
n + n { [ ( i - 1 ) + 2 ( j - 1 ) ] mod n }
Data Registers: R00 = n
>>> R01 thru Rn2 = the elements of the square if flag F00 is clear
Flags: F00-F01
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02
.............
CF 01 = Magic squares of order n where n is odd.
SF 01 = Panmagic squares of order n provided n mod 2
# 0 and n mod 3 # 0
Subroutines: /
01 LBL "MGSQ" 02 STO 00 03 X^2 04 STO M 05 LBL 01 06 RCL M 07 1 08 - 09 STO Y 10 RCL 00 |
11 / 12 INT 13 ENTER^ 14 ST+ X 15 FS? 01 16 GTO 01 17 RCL Z 18 + 19 X<> Z 20 + |
21 RCL 00
22 2 23 / 24 INT 25 + 26 1 27 GTO 02 28 LBL 01 29 ST+ Y 30 RCL Z |
31 LBL 02 32 ST+ Z 33 + 34 RCL 00 35 MOD 36 LASTX 37 * 38 X<>Y 39 LASTX 40 MOD |
41 + 42 1 43 + 44 FC? 00 45 STO IND M 46 FS? 00 47 VIEW X 48 DSE M 49 GTO 01 50 END |
( 77 bytes / SIZE 001 or 1+n^2 )
STACK | INPUTS | OUTPUTS |
X | n | / |
n mod 2 # 0
if CF 01 - magic square
n mod 2 # 0 & n mod 3 # 0 if SF 01 -
Panmagic square
Examples:
• CF 01 n = 3
3 XEQ "MGSQ" the HP-41 displays 2 7 6 9 5 1 4 3 8 if SF 00
-The magic square is
8 1 6
3 5 7
4 9 2
-Magic Constant = 15
• SF 01 n = 5
5 XEQ "MGSQ" the HP-41 displays 12 6 5 24 18 .................... 25 19 13 7 1 if SF 00
-The panmagic square is
01 14 22 10
18
07 20 03 11
24
13 21 09 17
05
19 02 15 23
06
25 08 16 04
12
-Magic Constant = 65
Note:
-If flag F00 is set, the elements are displayed from the last one to the
first.
-Lines 06-07 may be replaced by RCL 00 X^2
RCL M
but in this case, the elements will be stored in the reverse order
if CF 00
b) More Panmagic-Squares
( prime order > 4 )
-Here, we assume that the order is a prime p > 3
-Panmagic Squares may be created by the following formulae:
ai,j = 1 + [ b ( i - 1 ) + d ( j - 1 ) ] mod p + p { [ a ( i - 1 ) + c ( j - 1 ) ] mod p }
provided a , b , c , d verify the properties:
a , b , c , d # 0 mod p
a + c , a - c # 0 mod p
b + d , b - d # 0 mod p
a.d - b.c # 0 mod p
Data Registers: R00 = p R01 to R12 are also used for temporary data storage.
>>> R01 thru Rp2 = the elements of the panmagic square if flag F00 is clear
Flag: F00
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02
............
Subroutines: /
01 LBL "PMAG+" 02 "ORDER?" 03 PROMPT 04 STO 00 05 LBL 00 06 "A^B^C^D" 07 PROMPT 08 STO 04 09 STO 08 10 STO 09 11 RDN 12 STO 03 13 STO 06 14 STO 07 15 RDN 16 STO 02 17 ST+ 08 18 ST- 09 19 X<>Y |
20 STO 01 21 ST+ 06 22 ST- 07 23 RCL 04 24 * 25 RCL 02 26 RCL 03 27 * 28 - 29 STO 05 30 9 31 STO 10 32 LBL 01 33 RCL IND 10 34 RCL 00 35 MOD 36 X=0? 37 GTO 00 38 DSE 10 |
39 GTO 01 40 LASTX 41 X^2 42 STO 10 43 LBL 02 44 RCL 10 45 1 46 - 47 STO 11 48 RCL 00 49 / 50 INT 51 STO 12 52 RCL 03 53 * 54 RCL 01 55 RCL 11 56 * 57 + |
58 RCL 00 59 MOD 60 LASTX 61 * 62 RCL 02 63 RCL 11 64 * 65 RCL 04 66 RCL 12 67 * 68 + 69 RCL 00 70 MOD 71 + 72 1 73 + 74 FS? 00 75 VIEW X 76 FS? 00 |
77 GTO 02 78 RCL 10 79 12 80 + 81 X<>Y 82 STO IND Y 83 LBL 02 84 DSE 10 85 GTO 02 86 RCL 00 87 X^2 88 E6 89 / 90 13.001 91 + 92 FS? 00 93 CLX 94 REGMOVE 95 END |
( 142 bytes / SIZE 013 or 13+n^2 )
STACK | INPUTS | OUTPUTS |
X | / | / |
Example: You want to create a panmagic
square of order 5 with a = 1 , b = 2 , c = 3 , d = 4
XEQ "PMAG+" >>>> "ORDER?"
5 R/S
>>>> "A^B^C^D"
1 ENTER^
2 ENTER^
3 ENTER^
4 R/S
>>>> HP-41 displays 10
3 21 19 12 .......................
24 17 15 8 1
if SF 00
-If flag F00 is clear, you get:
R01 R06 R11 R16
R21 01
20 09 23 12
R02 R07 R12 R17
R22 08
22 11 05 19
R03 R08 R13 R18
R23 = 15 04
18 07 21
R04 R09 R14 R19
R24 17
06 25 14 03
R05 R10 R15 R20
R25 24
13 02 16 10
Notes:
-With n = 5, if you choose (a,b,c,d) = (1,1,2,3) , you'll get the
same panmagic square as in the above paragraph.
-If you choose a b c d values that don't satisfy the required properties,
the HP-41 displays "A^B^C^D" again.
-This routine does not check if p is a prime > 3, so line 02 may be
replaced by a more explicit "PRIMEORDER>3"
-If you never want to store the elements, replace lines 74 to 94 by VIEW X DSE 10 GTO 02
-Instead of placing the keys a , b , c , d in the stack, you can also let
your HP-41 choose these numbers at your place:
Data Registers: R00 = p R05 to R08: temp ( R08 = random numbers )
R01 = a R03 = c
R02 = b R04 = d
Flags: /
Subroutines: /
01 LBL "PMAG+" 02 STO 00 03 X<>Y 04 STO 08 05 LBL 00 06 4 07 STO 05 08 LBL 11 09 RCL 08 10 R-D 11 FRC 12 STO 08 13 RCL 00 14 DSE X 15 * 16 INT 17 1 18 + 19 STO IND 05 20 DSE 05 |
21 GTO 11 22 RCL 01 23 RCL 04 24 * 25 RCL 02 26 RCL 03 27 * 28 - 29 RCL 00 30 MOD 31 X=0? 32 GTO 00 33 RCL 01 34 RCL 03 35 + 36 RCL 00 37 MOD 38 X=0? 39 GTO 00 40 RCL 01 |
41 RCL 03 42 - 43 RCL 00 44 MOD 45 X=0? 46 GTO 00 47 RCL 02 48 RCL 04 49 + 50 RCL 00 51 MOD 52 X=0? 53 GTO 00 54 RCL 02 55 RCL 04 56 - 57 RCL 00 58 MOD 59 X=0? 60 GTO 00 |
61 LBL 10 62 RCL 00 63 X^2 64 STO 05 65 LBL 12 66 RCL 00 67 X^2 68 RCL 05 69 - 70 STO 06 71 RCL 00 72 / 73 INT 74 STO 07 75 RCL 03 76 * 77 RCL 01 78 RCL 06 79 * 80 + |
81 RCL 00 82 MOD 83 LASTX 84 * 85 RCL 02 86 RCL 06 87 * 88 RCL 04 89 RCL 07 90 * 91 + 92 RCL 00 93 MOD 94 + 95 1 96 + 97 VIEW X 98 DSE 05 99 GTO 12 100 END |
( 122 bytes / SIZE 009 )
STACK | INPUTS | OUTPUTS |
Y | r | / |
X | p | / |
where r = random seed and p is a prime > 3
Example: p = 5 , random seed = sqrt(2)
2 SQRT
5 XEQ "PMAG+" >>>>
1 17 8 24 15 ......................................
16 7
-The panmagic square is
01 09 12 20
23
17 25 03 06
14
08 11 19 22
05
24 02 10 13
16
15 18 21 04
07
-Here, the keys are 3 1 1 3 in registers R01 thru R04
Notes:
-Press XEQ 10 to view the elements again ( otherwise, line
61 LBL 10 may be deleted )
-Set F21 and the program will stop at each VIEW X
-Press XEQ 00 to search another magic square with the same
order and the random seed in R08
-You can replace line 97 by SAVEX if you have created a DATA file in X-memory.
-It may take some time before the HP-41 finds valid keys, especially for
small p-values.
-If you have an M-Code function RAND, lines 03 to 12 may be replaced by
LBL 00 4 STO 05
LBL 11 RAND
c) Panmagic Squares of
order n = 4 m
-The method is more complicated !
Formula:
-We calculate
[ ( i - 1 ) + ( n/2 ) ( j - 1 ) ] mod n = x
if x >= n/2 x' = 3n/2 - x - 1 &
if x < n/2 , x' = x
[ ( n/2 ) ( i - 1 ) + ( i - 1 ) ] mod n = y
if y >= n/2 y' = 3n/2 - y - 1 &
if y < n/2 , y' = y
-Then, ai,j = 1 + n x' + y'
Data Registers: R00 = n
>>> R01 thru Rn2 = the elements of the square if flag F00 is clear
Flags: F00
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02
.............
Subroutines: /
01 LBL "PMAG4" 02 STO 00 03 X^2 04 STO O 05 LBL 01 06 RCL O 07 1 08 - 09 STO N 10 RCL 00 11 / 12 INT |
13 X<> N 14 RCL 00 15 MOD 16 STO M 17 LASTX 18 2 19 / 20 STO Z 21 ST* M 22 RCL N 23 ST+ M 24 * |
25 + 26 RCL 00 27 MOD 28 X<Y? 29 GTO 00 30 - 31 + 32 + 33 DSE X 34 LBL 00 35 RCL 00 36 * |
37 X<> M 38 LASTX 39 MOD 40 X<Y? 41 GTO 00 42 - 43 + 44 + 45 DSE X 46 LBL 00 47 RCL M 48 + |
49 1 50 + 51 FC? 00 52 STO IND O 53 FS? 00 54 VIEW X 55 DSE O 56 GTO 01 57 END |
( 88 bytes / SIZE 001 or 1+n^2 )
STACK | INPUTS | OUTPUTS |
X | n | 1 |
where n is a multiple of 4
Example: n = 4
4 XEQ "PMAG4" the HP-41 displays successively 6 3 10 15 .............. 12 13 8 1 if SF 00
-The whole panmagic square is:
01 14 04 15
08 11 05 10
13 02 16 03
12 07 09 06
-The magic constant = 34
Note:
-There is no normal panmagic squares of order 2 , 6 , 10 ,
....
d) Magic Squares of order
n = 4m
Formulae:
-Let x' = 0 if 1 <= x <= n/2
and x' = 1 if n/2 < x <= n
-Let x" = n + 1 - x
-If ( i + j + i' + j' ) mod 2 = 1 then ai,j
= n ( i - 1 ) + j
-If ( i + j + i' + j' ) mod 2 = 0 then ai,j
= n ( i" - 1 ) + j"
Data Registers: R00 = n
>>> R01 thru Rn2 = the elements of the square if flag F00 is clear
Flags: F00
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02
.............
Subroutines: /
01 LBL "MGSQ4" 02 STO 00 03 X^2 04 STO N 05 LBL 01 06 RCL N 07 1 08 - 09 STO M 10 RCL 00 11 ST/ M 12 MOD 13 X<> M |
14 INT 15 RCL M 16 RCL 00 17 2 18 / 19 - 20 X=0? 21 ISG X 22 "" 23 X<0? 24 CLX 25 X>0? 26 ST/ X |
27 ST+ Z 28 X<> L 29 - 30 X<0? 31 FS? 30 32 SIGN 33 X<0? 34 CLX 35 + 36 RCL M 37 + 38 2 39 MOD |
40 X=0? 41 GTO 01 42 X<> M 43 GTO 00 44 LBL 01 45 X<>Y 46 CHS 47 RCL M 48 CHS 49 RCL 00 50 DSE X 51 ST+ Z 52 + |
53 LBL 00 54 RCL 00 55 * 56 + 57 1 58 + 59 FC? 00 60 STO IND N 61 FS? 00 62 VIEW X 63 DSE N 64 GTO 01 65 END |
( 99 bytes / SIZE 001 or 1+n^2 )
STACK | INPUTS | OUTPUTS |
X | n | 1 |
where n is a multiple of 4
Example: n = 4
4 XEQ "MGSQ4" the HP-41 displays successively 1 12 8 13 ................. 4 9 5 16 if SF 00
-The whole magic square is:
16 02 03 13
05 11 10 08
09 07 06 12
04 14 15 01
-The magic constant = 34
e) Magic Squares of order
n = 4m+2
-Singly-even order magic squares exist, but the formulas are complicated.
-"MGSQ6" uses the following ones:
Let t = n/2
u = ( i' - j' ) mod t
where x' = min
{ x , n+1-x }
v = 2 i" + j"
and x"
= 0 if 0 < x <= n/2 & x" = 1 if
n/2 < x <= n
Then, d(u,v) is defined by the table ( in the last 2 rows,
x > 0 so that u > 2 ):
d(u,0) | d(u,1) | d(u,2) | d(u,3) | |
d(0,v) | 3 | 1 | 2 | 0 |
d(1,v) | 1 | 3 | 0 | 2 |
d(2,v) | 0 | 1 | 3 | 2 |
d(2x+1,v) | 0 | 1 | 2 | 3 |
d(2x+2,v) | 3 | 2 | 1 | 0 |
Finally, ai,j = t2 d(u,v)
+ t [ ( i' - j' + (t-1)/2 ) mod t ] +
[ ( i' + j' - (t+1)/2 ) mod t ] + 1
Data Registers: R00 = n
R01 thru R08: temp
Flags: /
Subroutines: /
-Line 147 is a three-byte GTO 01
01 LBL "MGSQ6" 02 STO 00 03 X^2 04 STO 03 05 LBL 01 06 RCL 03 07 1 08 STO 04 09 STO 05 10 - 11 STO 01 12 RCL 00 13 MOD 14 X<> 01 15 LASTX 16 / 17 INT 18 STO 02 19 RCL 00 20 2 21 / 22 STO 06 23 X>Y? 24 DSE 05 25 CLX 26 RCL 01 27 RCL 06 28 X>Y? 29 DSE 04 30 CLX |
31 RCL 00 32 RCL 01 33 ST- Y 34 1 35 + 36 X>Y? 37 X<>Y 38 STO 07 39 RCL 00 40 RCL 02 41 ST- Y 42 LASTX 43 + 44 X>Y? 45 X<>Y 46 STO 08 47 RCL 07 48 - 49 RCL 06 50 MOD 51 X<> 04 52 ST+ X 53 RCL 05 54 + 55 STO 05 56 2 57 MOD 58 STO 01 59 LASTX 60 RCL 04 |
61 X>Y? 62 GTO 01 63 X=Y? 64 GTO 03 65 X=0? 66 GTO 05 67 RCL 01 68 5 69 * 70 2 71 + 72 RCL 05 73 - 74 2 75 / 76 GTO 06 77 LBL 01 78 X<>Y 79 MOD 80 X=0? 81 GTO 02 82 RCL 05 83 GTO 06 84 LBL 02 85 3 86 RCL 05 87 - 88 GTO 06 89 LBL 03 90 RCL 01 |
91 X=0? 92 GTO 04 93 RCL 05 94 + 95 2 96 / 97 GTO 06 98 LBL 04 99 RCL 05 100 3 101 * 102 2 103 / 104 GTO 06 105 LBL 05 106 6 107 RCL 01 108 3 109 * 110 - 111 RCL 05 112 - 113 2 114 / 115 LBL 06 116 RCL 06 117 X^2 118 * 119 RCL 06 120 2 |
121 / 122 INT 123 STO 01 124 RCL 07 125 + 126 RCL 08 127 - 128 RCL 06 129 MOD 130 LASTX 131 * 132 RCL 07 133 RCL 08 134 + 135 RCL 01 136 - 137 1 138 - 139 RCL 06 140 MOD 141 + 142 + 143 1 144 + 145 VIEW X 146 DSE 03 147 GTO 01 148 END |
( 179 bytes / SIZE 009 )
STACK | INPUTS | OUTPUTS |
X | n | / |
where n > 2 is a multiple of 2 but not a multiple of 4
Example: n = 6
6 XEQ "MGSQ6" the HP-41 displays successively 4 26 21 30 17 13 .............. 22 35 3 12 8 31
-So, we get an example of sixth-order magic square is
04 26 21 30
17 13
20 06 25 16
15 29
27 19 05 14
28 18
36 01 23 32
10 09
02 24 34 07
33 11
22 35 03 12
08 31
Notes:
-You may of course change the order of the elements.
-If you want to store these numbers,
add RCL 00 X^2 E6
/ 9.001 + REGMOVE
after line 147
and replace line 145 by RCL 03
8 + X<>Y STO IND Y
-I found these formulae - after some trial and error - by analogy with
those given in reference [4] for creating singly-even order magic cubes.
-They work well for magic squares of order 6 10 14
18 22 ( I've checked )
but I cannot guarantee they are valid in all cases ( though I would
be surprised if they were not ! )
f) Bimagic Squares
"BMAG" constructs a bimagic square ( a magic square that remains magic after squaring all its elements ) of order p2 provided p is a prime p > 2
If p > 3 , the bimagic square is also panmagic.
-The method uses 4 key-numbers: r = ( a b c d )p
r' = ( a' b' c' d' )p r" = ( a" b" c" d" )p
r''' = ( a''' b''' c''' d''' )p witten in base p
-The element ai,j is given by
ai,j = p3 [ { a [ ( i -
1 ) mod p ] + a' int [ ( i - 1 ) / p ] + a" [ ( j - 1 ) mod p ] +
a''' int [ ( j - 1 ) / p ] } mod p ]
+ p2 [
{ b [ ( i - 1 ) mod p ] + b' int [ ( i - 1 ) / p ] + b" [
( j - 1 ) mod p ] + b''' int [ ( j - 1 ) / p ] } mod p ]
+ p [
{ c [ ( i - 1 ) mod p ] + c' int [ ( i - 1 ) / p ] + c"
[ ( j - 1 ) mod p ] + c''' int [ ( j - 1 ) / p ] } mod p ]
+ [
{ d [ ( i - 1 ) mod p ] + d' int [ ( i - 1 ) / p
] + d" [ ( j - 1 ) mod p ] + d''' int [ ( j - 1 ) / p ] } mod p ] + 1
-This formula will give a bimagic square if the following determinant is different from 0 ( mod p )
a b c
d
a' b' c' d'
a" b" c" d"
a''' b''' c''' d'''
and if a b' - a' b # 0 , a c' - a' c # 0 , a d'
- a' d # 0 , b c' - b' c # 0 , ...... , c d' - c' d # 0
( mod p )
and similar relations with the keys r" & r'''
-The square will be panmagic if the keys
r + r" & r' + r'''
and
r - r" & r' - r'''
satisfy the same similar relations too.
-In the following program, the 4 keys are:
( 2 1 0 1 ) ( 0 1 1 2 ) ( 1 0 2 1 ) ( 2
1 2 0 ) if p = 3
( 0 1 1 1 ) ( 1 0 1 2 ) ( 3 3 2 1 ) ( 2
1 0 1 ) if p > 3 , p # 11
( 0 1 1 1 ) ( 1 0 1 2 ) ( 3 3 2 0 ) ( 2
1 0 1 ) if p = 11
-But other choices are clearly posible.
Data Registers: R00 = p2 R05 = p R06 = k = p4 , p4 - 1 , ..... , 2 , 1
R01 = ( i - 1 ) mod p
R03 = ( j - 1 ) mod p
with i - 1 = ( k - 1 ) mod p2
R02 = int [ ( i - 1 ) / p ]
R04 = int [ ( j - 1 ) / p ]
and j - 1 = int [ ( k - 1 ) / p2
]
Flags: F09-F10
Subroutines: /
-Line 119 is a three-byte GTO 01
01 LBL "BMAG" 02 STO 05 03 X^2 04 STO 00 05 X^2 06 STO 06 07 CF 09 08 CF 10 09 3 10 RCL 05 11 X=Y? 12 SF 09 13 11 14 X=Y? 15 SF 10 16 LBL 01 17 RCL 06 18 1 19 - 20 STO 04 21 RCL 00 22 MOD 23 STO 02 24 RCL 05 25 MOD |
26 STO 01
27 ST+ X 28 FC? 09 29 CLST 30 RCL 02 31 RCL 05 32 / 33 INT 34 STO 02 35 FS? 09 36 CLX 37 + 38 RCL 04 39 RCL 00 40 / 41 INT 42 STO 04 43 RCL 05 44 MOD 45 STO 03 46 3 47 FS? 09 48 SIGN 49 * 50 + |
51 RCL 04 52 RCL 05 53 / 54 INT 55 STO 04 56 ST+ X 57 + 58 RCL 05 59 MOD 60 LASTX 61 * 62 RCL 01 63 RCL 02 64 FC? 09 65 CLX 66 + 67 RCL 03 68 3 69 FS? 09 70 CLX 71 * 72 + 73 RCL 04 74 + 75 RCL 05 |
76 MOD 77 + 78 RCL 05 79 * 80 RCL 02 81 RCL 01 82 FS? 09 83 CLX 84 + 85 RCL 03 86 ST+ X 87 + 88 RCL 04 89 ST+ X 90 FC? 09 91 CLX 92 + 93 RCL 05 94 MOD 95 + 96 RCL 05 97 * 98 RCL 01 99 RCL 02 100 ST+ X |
101 + 102 RCL 03 103 FC? 09 104 FS? 10 105 X<0? 106 CLX 107 + 108 RCL 04 109 FS? 09 110 CLX 111 + 112 RCL 05 113 MOD 114 + 115 1 116 + 117 VIEW X 118 DSE 06 119 GTO 01 120 CF 09 121 CF 10 122 END |
( 157 bytes / SIZE 007 )
STACK | INPUTS | OUTPUTS |
X | p | 1 |
where p is a prime, p > 2
Example: p = 3 We'll have a bimagic square of order 9
3 XEQ "BMAG" the HP-41 displays successively 33 77 13 46 ..................... 15 48 65 1
-The whole bimagic square is:
01 35 60 70
14 39 49 74 27
65 18 40 53
78 19 05 30 61
48 79 23 09
31 56 69 10 44
Magic Constant = 369
15 37 71 75
25 50 36 58 02
76 20 54 28
62 06 16 41 66
Magic Constant of the squared elements = 20049
32 57 07 11
45 67 80 24 46
26 51 73 59
03 34 38 72 13
63 04 29 42
64 17 21 52 77
43 68 12 22
47 81 55 08 33
-The elements are actually displayed from the last one to the first one,
but it's only a convention.
-If you prefer the reverse order, replace lines 17-18 by RCL 00
X^2 RCL 06
2°) Magic Cubes
a) Magic & Panmagic
Cubes
-The following programs create normal magic cubes, i-e their elements are 1 2 3 ............... n3
-An Andrews magic cube is a cubic array where the sums of the elements
equal the magic constant n(n3+1)/2 in the 3 directions and
the 4 space diagonals.
-The cube is perfect pandiagonal if all the orthogonal sections are panmagic
squares.
-In a panmagic cube , all the orthogonal or diagonal sections - broken
or unbroken - are panmagic squares.
"MGCB" builds:
Andrews magic cubes if n = 3 or 5 and - more generally
- if n is odd and flag F01 is clear.
Perfect pandiagonal magic cube if n = 7 & SF 01
Panmagic cubes if n is a prime > 7 & SF 01
Formulae:
ai,j,k = n2 { [ a
( i - 1 ) + a' ( j - 1 ) + a" ( k - 1 ) ] mod n }
+ n { [ b ( i - 1 ) + b' ( j - 1 ) + b" ( k - 1 )
] mod n }
+ [ c ( i - 1 ) + c' ( j - 1 ) + c" ( k - 1 ) ] mod n + 1
with
( a b c ) = ( 1 -1 1 )
( a' b' c' ) = ( -1 1 1 )
to create Andrews Magic Cubes of odd orders
( a" b" c" ) = ( 1 1 -1 )
( a b c ) = ( 6 5 4 )
( a' b' c' ) = ( 5 4 5 )
if n = 7 & SF 01
( a" b" c" ) = ( 4 6 6 )
( a b c ) = ( 1 1 1 )
( a' b' c' ) = ( 3 2 2 )
if n is a prime > 7 & SF 01
( a" b" c" ) = ( 5 5 4 )
Data Registers: R00 = n is an odd integer
>>> R01 thru Rn3 = the elements of the cube if flag F00 is clear
Flags: F00-F01
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02 .............
Subroutines: /
-Line 107 is a three-byte GTO 01
01 LBL "MGCB" 02 STO 00 03 3 04 Y^X 05 STO O 06 LBL 01 07 RCL O 08 1 09 - 10 ENTER^ 11 STO N 12 RCL 00 13 MOD 14 STO M 15 X<> L 16 X^2 17 MOD 18 RCL 00 19 / 20 INT 21 X<> N 22 RCL 00 |
23 X^2 24 / 25 INT 26 STO Y 27 RCL 00 28 7 29 - 30 FS? 01 31 X<0? 32 GTO 01 33 X=0? 34 GTO 02 35 RDN 36 ST+ X 37 RCL N 38 + 39 ST+ X 40 RCL M 41 + 42 ENTER^ 43 R^ 44 + |
45 RCL X 46 RCL N 47 GTO 03 48 LBL 01 49 RDN 50 CHS 51 RCL M 52 + 53 RCL N 54 + 55 ENTER^ 56 CHS 57 RCL X 58 LASTX 59 ST+ X 60 + 61 X<>Y 62 RCL M 63 ST+ X 64 GTO 03 65 LBL 02 66 CLX |
67 RCL M 68 RCL N 69 + 70 ST+ X 71 + 72 CHS 73 RCL M 74 CHS 75 X<>Y 76 ST+ Y 77 RCL N 78 CHS 79 X<>Y 80 ST+ Y 81 R^ 82 ST+ X 83 - 84 RCL M 85 LBL 03 86 + 87 RCL 00 88 MOD |
89 LASTX 90 * 91 X<>Y 92 LASTX 93 MOD 94 + 95 RCL 00 96 * 97 X<>Y 98 LASTX 99 MOD 100 + 101 1 102 + 103 FC? 00 104 STO IND O 105 FS? 00 106 VIEW X 107 DSE O 108 GTO 01 109 END |
( 158 bytes / SIZE 001 or 1+n^3 )
STACK | INPUTS | OUTPUTS |
X | n | 1 |
Example: With n = 3
3 XEQ "MGCB" the HP-41 displays 27 11 4 ................... 24 17 1 if SF 00
-If CF 00, we get in registers R01 thru R27
R01 R04 R07
01 23 18
R02 R05 R08 = 17
03 22
R03 R06 R09
24 16 02
R10 R13 R16 15
07 20
R11 R14 R17 = 19 14
09
R12 R15 R18 08
21 13
R19 R22 R25 26
12 04
R20 R23 R26 = 06 25
11
R21 R24 R27 10
05 27
-The magic constant = 42
Note:
-Clearing or setting F01 has no effect if n = 3 or n = 5.
b) More Panmagic-Cubes
( prime order > 10 )
-The formula
ai,j,k = n2 { [ a
( i - 1 ) + a' ( j - 1 ) + a" ( k - 1 ) ] mod n }
+ n { [ b ( i - 1 ) + b' ( j - 1 ) + b" ( k - 1 )
] mod n }
+ [ c ( i - 1 ) + c' ( j - 1 ) + c" ( k - 1 ) ] mod n + 1
will produce a panmagic cube provided the following relations are satisfied:
| a
a' a" |
Det | b b' b" |
# 0 mod p
| c
c' c" |
a , a' , a" , a + a' , a - a' , a + a" , a - a" , a' + a" , a' - a" , a + a' + a" , -a + a' + a" , a - a' + a" , a + a' - a" # 0 mod p
and similar relations with b and c
-"PMGC+" choose 9 numbers at random between 1 & p-1
and tests if these properties are satisfied.
-Otherwise, 9 other numbers are created and so on ...
-When valid keys are found, the successive elements of the panmagic cube
are displayed.
Data Registers: R00 = p R10 to R15: temp ( R15 = random numbers )
R01 = a R04 = a' R07 = a"
R02 = b R05 = b' R08 = b"
R03 = c R06 = c' R09 = c"
Flags: /
Subroutines: /
-Lines 99-106-141 are three-byte GTO 00
-Lines 109 to 137 may be replaced by XEQ "D3" ( cf "Determinants
for the HP-41" )
01 LBL "PMGC+" 02 STO 00 03 X<>Y 04 STO 15 05 LBL 00 06 9 07 STO 10 08 LBL 11 09 RCL 15 10 R-D 11 FRC 12 STO 15 13 RCL 00 14 DSE X 15 * 16 INT 17 1 18 + 19 STO IND 10 20 DSE 10 21 GTO 11 22 3 23 STO 10 24 LBL 12 25 RCL 07 26 X<> 08 27 X<> 09 28 STO 07 29 RCL 04 30 X<> 05 31 X<> 06 32 STO 04 33 RCL 01 34 X<> 02 35 X<> 03 36 STO 01 37 + |
38 STO 11 39 RCL 00 40 MOD 41 X=0? 42 GTO 00 43 RCL 01 44 RCL 04 45 - 46 STO 12 47 RCL 00 48 MOD 49 X=0? 50 GTO 00 51 RCL 01 52 RCL 07 53 + 54 RCL 00 55 MOD 56 X=0? 57 GTO 00 58 RCL 01 59 RCL 07 60 - 61 RCL 00 62 MOD 63 X=0? 64 GTO 00 65 RCL 04 66 RCL 07 67 + 68 RCL 00 69 MOD 70 X=0? 71 GTO 00 72 RCL 04 73 RCL 07 74 - |
75 RCL 00
76 MOD 77 X=0? 78 GTO 00 79 RCL 07 80 RCL 11 81 + 82 RCL 00 83 MOD 84 X=0? 85 GTO 00 86 RCL 07 87 RCL 11 88 - 89 RCL 00 90 MOD 91 X=0? 92 GTO 00 93 RCL 07 94 RCL 12 95 + 96 RCL 00 97 MOD 98 X=0? 99 GTO 00 100 RCL 07 101 RCL 12 102 - 103 RCL 00 104 MOD 105 X=0? 106 GTO 00 107 DSE 10 108 GTO 12 109 RCL 05 110 RCL 09 111 * |
112 RCL 06 113 RCL 08 114 * 115 - 116 RCL 01 117 * 118 RCL 02 119 RCL 09 120 * 121 RCL 03 122 RCL 08 123 * 124 - 125 RCL 04 126 * 127 - 128 RCL 02 129 RCL 06 130 * 131 RCL 03 132 RCL 05 133 * 134 - 135 RCL 07 136 * 137 + 138 RCL 00 139 MOD 140 X=0? 141 GTO 00 142 LBL 10 143 RCL 00 144 3 145 Y^X 146 STO 10 147 STO 14 148 LBL 13 |
149 RCL 14 150 RCL 10 151 - 152 STO 12 153 RCL 00 154 X^2 155 / 156 INT 157 STO 13 158 RCL 12 159 RCL 00 160 X^2 161 MOD 162 RCL 00 163 / 164 INT 165 X<> 12 166 RCL 00 167 MOD 168 STO 11 169 RCL 01 170 * 171 RCL 04 172 RCL 12 173 * 174 + 175 RCL 07 176 RCL 13 177 * 178 + 179 RCL 00 180 MOD 181 LASTX 182 * 183 RCL 02 184 RCL 11 185 * |
186 RCL 05 187 RCL 12 188 * 189 + 190 RCL 08 191 RCL 13 192 * 193 + 194 RCL 00 195 MOD 196 + 197 RCL 00 198 * 199 RCL 03 200 RCL 11 201 * 202 RCL 06 203 RCL 12 204 * 205 + 206 RCL 09 207 RCL 13 208 * 209 + 210 RCL 00 211 MOD 212 + 213 1 214 + 215 VIEW X 216 DSE 10 217 GTO 13 218 END |
( 258 bytes / SIZE 016 )
STACK | INPUTS | OUTPUTS |
Y | r | / |
X | p | / |
where r = random seed and p is a prime > 10
Example: p = 41 , random seed = 1
1 ENTER^
41 XEQ "PMGC+" >>>>
1 8485 16928 25371 33814
...................................... 30339 38782
47225 55668 64111.
-The keys are in registers R01 thru R09
(a,b,c) = ( 5 1
38 ) = R01 R02 R03
(a',b',c') = ( 26 14 13 ) = R04
R05 R06
(a",b",c") = ( 13 21 4 ) = R07
R08 R09
Notes:
-Press XEQ 10 to view the elements again ( otherwise, line
142 LBL 10 may be deleted )
-Set F21 and the program will stop at each VIEW X
-Press XEQ 00 to search another magic square with the same
order and the random seed already in R15
-You can replace line 215 by HSAVEX if you have created an HEPAX DATA file - and if n is not too large...
-It may take a long time before the HP-41 finds valid keys, especially
if p = 11 or 13.
-If you have an M-Code function RAND, lines 03 to 12 may be replaced by
LBL 00 9 STO 10 LBL 11 RAND
-If you want to use your own keys, initialize R00 thru R09, add
LBL 09 after line 21
and press XEQ 09 to check the keys or directly
XEQ 10 to display the elements of the panmagic cube.
( Change the first lines according to your preferences to get more
explicit PROMPTs )
c) Magic Cubes of order
n = 4 m
Formulae:
-Let x' = 0 if 1 <= x <= n/2
and x' = 1 if n/2 < x <= n
-Let x" = n + 1 - x
-If ( i + j + k + i' + j' + k' ) mod 2 = 1 then
ai,j,k = n2 ( i - 1 ) + n ( j - 1 ) + k
-If ( i + j + k + i' + j' + k' ) mod 2 = 0 then
ai,j,k = n2 ( i" - 1 ) + n ( j" - 1 ) + k"
Data Registers: R00 = n
>>> R01 thru Rn3 = the elements of the cube if flag F00 is clear
Flag: F00
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02 .............
Subroutines: /
01 LBL "MGCB4" 02 STO 00 03 3 04 Y^X 05 STO O 06 LBL 01 07 RCL O 08 1 09 - 10 ENTER^ 11 STO N 12 RCL 00 13 MOD 14 STO M 15 X<> L 16 X^2 17 MOD 18 RCL 00 19 / 20 INT |
21 X<> N 22 RCL 00 23 X^2 24 / 25 INT 26 RCL M 27 RCL 00 28 2 29 / 30 - 31 X=0? 32 ISG X 33 "" 34 X<0? 35 CLX 36 X>0? 37 ST/ X 38 ST+ Z 39 X<> L 40 - |
41 X=0? 42 ISG X 43 "" 44 X<0? 45 CLX 46 X>0? 47 ST/ X 48 ST+ Y 49 CLX 50 RCL N 51 LASTX 52 - 53 X<0? 54 FS? 30 55 SIGN 56 X<0? 57 CLX 58 + 59 RCL M 60 + |
61 RCL N 62 + 63 2 64 MOD 65 X=0? 66 GTO 00 67 X<>Y 68 CHS 69 RCL N 70 CHS 71 RCL M 72 CHS 73 RCL 00 74 DSE X 75 ST+ T 76 ST+ Z 77 + 78 GTO 01 79 LBL 00 80 X<> N |
81 RCL M 82 LBL 01 83 RCL 00 84 * 85 + 86 RCL 00 87 * 88 + 89 1 90 + 91 FC? 00 92 STO IND O 93 FS? 00 94 VIEW X 95 DSE O 96 GTO 01 97 END |
( 141 bytes / SIZE 001 or 1+n^3 )
STACK | INPUTS | OUTPUTS |
X | n | / |
Example: With n = 4
4 XEQ "MGCB4" the HP-41 displays 64 17 33 16 ................... 49 32 48 1 if SF 00
-If CF 00 we get:
R01 = 1 R02 = 48 R03 = 32 R04 = 49
........................ R63 = 17 R64 = 64
d) Magic Cubes of order
n = 6 , 10 , 14 , ....
-Now we assume that the order n = 2 ( mod 4 ) , n > 2
-The formulas are more complex. They may be found in reference [4]
-"MGCB6" displays the successive elements.
Data Registers:
R00 = n
R01 to R08: temp
Flags: /
Subroutines: /
-Line 187 is a three-byte GTO 01
01 LBL "MGCB6" 02 STO 00 03 3 04 Y^X 05 STO 04 06 LBL 01 07 RCL 04 08 1 09 STO 06 10 STO 07 11 STO 08 12 - 13 STO 02 14 RCL 00 15 X^2 16 / 17 INT 18 STO 03 19 RCL 02 20 RCL 00 21 X^2 22 MOD 23 RCL 00 24 / 25 INT 26 X<> 02 27 RCL 00 28 MOD 29 STO 01 30 LASTX 31 2 32 / 33 STO 05 34 X>Y? 35 DSE 06 36 CLX 37 RCL 02 38 RCL 05 |
39 X>Y? 40 DSE 07 41 CLX 42 RCL 03 43 RCL 05 44 X>Y? 45 DSE 08 46 CLX 47 RCL 00 48 RCL 01 49 ST- Y 50 1 51 + 52 X>Y? 53 X<>Y 54 STO 01 55 RCL 00 56 RCL 02 57 ST- Y 58 LASTX 59 + 60 X>Y? 61 X<>Y 62 STO 02 63 RCL 00 64 RCL 03 65 ST- Y 66 LASTX 67 + 68 X>Y? 69 X<>Y 70 STO 03 71 RCL 02 72 - 73 RCL 01 74 + 75 RCL 05 76 MOD |
77 X<> 06 78 ST+ X 79 RCL 07 80 + 81 ST+ X 82 RCL 08 83 + 84 STO 07 85 2 86 MOD 87 STO 08 88 LASTX 89 RCL 06 90 X>Y? 91 GTO 01 92 X=Y? 93 GTO 03 94 X=0? 95 GTO 05 96 RCL 08 97 9 98 * 99 6 100 + 101 RCL 07 102 - 103 2 104 / 105 GTO 06 106 LBL 01 107 X<>Y 108 MOD 109 X=0? 110 GTO 02 111 RCL 07 112 GTO 06 113 LBL 02 114 7 |
115 RCL 07 116 - 117 GTO 06 118 LBL 03 119 RCL 08 120 X=0? 121 GTO 04 122 RCL 07 123 X^2 124 7 125 + 126 8 127 / 128 GTO 06 129 LBL 04 130 14 131 RCL 07 132 ST- Y 133 * 134 8 135 / 136 GTO 06 137 LBL 05 138 14 139 RCL 08 140 7 141 * 142 - 143 RCL 07 144 - 145 2 146 / 147 LBL 06 148 RCL 05 149 3 150 Y^X 151 * 152 RCL 01 |
153 RCL 02 154 - 155 STO 06 156 RCL 03 157 + 158 1 159 - 160 RCL 05 161 MOD 162 LASTX 163 * 164 RCL 06 165 RCL 03 166 - 167 RCL 05 168 MOD 169 + 170 RCL 05 171 * 172 RCL 01 173 RCL 02 174 + 175 RCL 03 176 + 177 2 178 - 179 RCL 05 180 MOD 181 + 182 + 183 1 184 + 185 VIEW X 186 DSE 04 187 GTO 01 188 END |
( 225 bytes / SIZE 009 )
STACK | INPUTS | OUTPUTS |
X | n | / |
Example: With n = 6
6 XEQ "MGCB6" the HP-41 displays 116 201 22 76 66 170 ........................... 35 147 157 211 12 89.
-The magic sum = 651
3°) Magic Squares and Cubes
-This routine combines the programs listed in §1-a) & §2-a)
Data Registers: R00 = n
>>> R01 thru Rn2 or Rn3 = the elements of the square or the cube if flag F00 is clear
Flags: F00-F01-F03
SF 00 = The elements are successively displayed
CF 00 = The elements are stored into R01 R02 .............
CF 03 = Magic Squares
CF 01 = Magic squares of order n where n
is odd.
SF 01 = Panmagic squares of order n where
n mod 2 # 0 and n mod 3 # 0
SF 03 = Magic Cubes
CF 01 = Andrews Magic cubes of order n
where n is odd.
SF 01 =
Andrews magic cube if
n = 3 or 5
perfect pandiagonal magic
cube if n = 7
Panmagic cube if n is
a prime > 7
Subroutines: /
-Line 139 is a three-byte GTO 01
01 LBL "MAGIC" 02 STO 00 03 ENTER^ 04 X^2 05 FS? 03 06 * 07 STO O 08 LBL 01 09 RCL O 10 1 11 - 12 ENTER^ 13 STO N 14 RCL 00 15 MOD 16 FS? 03 17 GTO 02 18 X<>Y 19 LASTX 20 / 21 INT 22 ENTER^ 23 ST+ X 24 FS? 01 25 GTO 01 26 RCL Z 27 + 28 X<> Z |
29 + 30 1 31 ST+ Z 32 + 33 RCL 00 34 2 35 / 36 INT 37 GTO 05 38 LBL 01 39 ST+ Y 40 RCL Z 41 ST+ Z 42 GTO 05 43 LBL 02 44 STO M 45 X<> L 46 X^2 47 MOD 48 RCL 00 49 / 50 INT 51 X<> N 52 RCL 00 53 X^2 54 / 55 INT 56 STO Y |
57 RCL 00
58 7 59 - 60 FS? 01 61 X<0? 62 GTO 03 63 X=0? 64 GTO 04 65 RDN 66 ST+ X 67 RCL N 68 + 69 ST+ X 70 RCL M 71 + 72 ENTER^ 73 R^ 74 + 75 RCL X 76 RCL N 77 GTO 05 78 LBL 03 79 RCL M 80 R^ 81 - 82 RCL N 83 + 84 ENTER^ |
85 CHS 86 RCL X 87 LASTX 88 ST+ X 89 + 90 X<>Y 91 RCL M 92 ST+ X 93 GTO 05 94 LBL 04 95 CLX 96 RCL M 97 RCL N 98 + 99 ST+ X 100 + 101 CHS 102 RCL M 103 CHS 104 X<>Y 105 ST+ Y 106 RCL N 107 CHS 108 X<>Y 109 ST+ Y 110 R^ 111 ST+ X 112 - |
113 RCL M 114 LBL 05 115 + 116 RCL 00 117 MOD 118 LASTX 119 * 120 X<>Y 121 LASTX 122 MOD 123 + 124 FC? 03 125 GTO 06 126 X<>Y 127 RCL 00 128 ST* Z 129 MOD 130 + 131 LBL 06 132 1 133 + 134 FC? 00 135 STO IND O 136 FS? 00 137 VIEW X 138 DSE O 139 GTO 01 140 END |
( 207 bytes / SIZE 001 or 1+n^2 or 1+n^3 )
STACK | INPUTS | OUTPUTS |
X | n | 1 |
Examples: See §1-a) & §2-a)
4°) A few Tests
a) Magic Square ?
-This program checks if a given square matrix is a magic square.
-The coefficients are to be stored in registers R01 thru Rn2
-The magic constant is returned in register X if the answer is YES
Data Registers: R00 = n ( Registers R01 thru Rn^2 are to be initialized before executing "MGSQ?" )
• R01 thru Rn2 = the elements ai,j
Flags: /
Subroutines: /
01 LBL "MGSQ?" 02 STO 00 03 X^2 04 LASTX 05 1 06 + 07 E5 08 / 09 + 10 0 11 LBL 00 12 RCL IND Y 13 + 14 DSE Y 15 GTO 00 16 STO O 17 RCL 00 18 X^2 19 LASTX |
20 1 21 - 22 ST- Y 23 1 24 % 25 + 26 E3 27 / 28 + 29 0 30 LBL 01 31 RCL IND Y 32 + 33 DSE Y 34 GTO 01 35 RCL O 36 X#Y? 37 GTO 04 38 RCL 00 |
39 X^2 40 LASTX 41 STO N 42 0 43 LBL 02 44 RCL IND Z 45 + 46 DSE Z 47 "" 48 DSE Y 49 GTO 02 50 RCL O 51 X#Y? 52 GTO 04 53 R^ 54 RCL 00 55 0 56 DSE N 57 GTO 02 |
58 RCL 00 59 STO N 60 X^2 61 LASTX 62 E5 63 / 64 + 65 STO M 66 RCL 00 67 0 68 LBL 03 69 RCL IND Z 70 + 71 DSE Z 72 "" 73 DSE Y 74 GTO 03 75 RCL O 76 X#Y? |
77 GTO 04 78 RCL M 79 1 80 - 81 STO M 82 RCL 00 83 0 84 DSE N 85 GTO 03 86 RCL O 87 "YES" 88 GTO 05 89 LBL 04 90 "NO" 91 LBL 05 92 AVIEW 93 END |
( 144 bytes / SIZE 1+n^2 )
STACK | INPUTS | OUTPUTS |
X | n | Magic Cste* |
* If the HP-41 displays YES at the end
Example: Store the following 6x6 array into R01 to R36 ( column order or raw order )
06 32 03 34 35 01
07 11 27 28 08 30
19 14 16 15 23 24
18 20 22 21 17 13
25 29 10 09 26 12
36 05 33 04 02 31
6 XEQ "MGSQ?" >>>> "YES" ---Execution time = 24s---
and X = magic constant = 111
b) Panmagic Square ?
-This program checks if a given square matrix is a panmagic square.
-The coefficients must be stored in registers R01 thru Rn2
-The magic constant is returned in register X if the answer is YES
Data Registers: R00 = n ( Registers R01 thru Rn^2 are to be initialized before executing "MGSQ?" )
• R01 thru Rn2 = the elements ai,j
Flag: F10
Subroutines: /
-Line 31 is a three-byte GTO 06 but it's not really important.
01 LBL "PMAG?" 02 STO 00 03 STO M 04 X^2 05 STO N 06 SF 10 07 LBL 00 08 0 09 LBL 01 10 RCL IND Y 11 + 12 RCL Y 13 RCL 00 14 MOD 15 1 16 ST- T 17 - 18 X=0? 19 GTO 01 20 CLX 21 RCL 00 22 ST- Z 23 LBL 01 24 RDN 25 DSE M 26 GTO 01 |
27 FS?C 10 28 STO O 29 RCL O 30 X#Y? 31 GTO 06 32 RCL 00 33 STO M 34 X^2 35 LASTX 36 - 37 DSE N 38 RCL N 39 X>Y? 40 GTO 00 41 RCL 00 42 X^2 43 STO N 44 LBL 02 45 0 46 LBL 03 47 RCL IND Y 48 + 49 RCL Y 50 RCL 00 51 ST- T 52 ISG T |
53 CLX 54 MOD 55 X#0? 56 GTO 03 57 X<> L 58 ST- Z 59 LBL 03 60 RDN 61 DSE M 62 GTO 03 63 RCL O 64 X#Y? 65 GTO 06 66 RCL 00 67 STO M 68 X^2 69 LASTX 70 - 71 DSE N 72 RCL N 73 X>Y? 74 GTO 02 75 RCL 00 76 X^2 77 LASTX 78 STO N |
79 0 80 LBL 04 81 RCL IND Z 82 + 83 DSE Z 84 "" 85 DSE Y 86 GTO 04 87 RCL O 88 X#Y? 89 GTO 06 90 R^ 91 RCL 00 92 0 93 DSE N 94 GTO 04 95 RCL 00 96 STO N 97 X^2 98 LASTX 99 E5 100 / 101 + 102 STO M 103 RCL 00 104 0 |
105 LBL 05 106 RCL IND Z 107 + 108 DSE Z 109 "" 110 DSE Y 111 GTO 05 112 RCL O 113 X#Y? 114 GTO 06 115 RCL M 116 1 117 - 118 STO M 119 RCL 00 120 0 121 DSE N 122 GTO 05 123 RCL O 124 "YES" 125 GTO 07 126 LBL 06 127 "NO" 128 LBL 07 129 AVIEW 130 END |
( 204 bytes / SIZE 1+n^2 )
STACK | INPUTS | OUTPUTS |
X | n | Magic Cste* |
* If the HP-41 displays YES at the end
Example: Store the following matrix into R01 to R36 ( column order or raw order )
067 193 071 251
109 239
139 233 113 181
157 107
241 097 191 089
163 149
073 167 131 229
151 179
199 103 227 101
127 173
211 137 197 079
223 083
6 XEQ "PMAG?" >>>> "YES" ---Execution time = 58s---
and X = magic constant = 930
-This panmagic square contains all the consecutive primes between 67 and
251
-It was found by Allan W. Johnson
c) Magic Cube ?
-This program checks if a given cubic array is an Andrews magic cube.
-The coefficients must be stored in registers R01 thru Rn3
-The magic constant is returned in register X if the answer is YES
Data Registers: R00 = n ( Registers R01 thru Rn^3 are to be initialized before executing "MGCB?" )
• R01 thru Rn3 = the elements ai,j,k
Flags: /
Subroutines: /
-Lines 25-41-51 are three-byte GTO 04, but it doesn't really matter.
01 LBL "MGCB?" 02 STO 00 03 ENTER^ 04 X^2 05 * 06 STO M 07 LASTX 08 RCL 00 09 + 10 STO N 11 1 12 + 13 XEQ 00 14 STO O 15 RCL M 16 RCL 00 17 - 18 RCL N 19 1 20 ST+ Z 21 - 22 XEQ 00 23 RCL O 24 X#Y? 25 GTO 04 26 RCL M 27 RCL 00 28 X^2 29 - 30 STO M 31 LASTX |
32 RCL 00
33 - 34 STO N 35 1 36 ST+ Z 37 - 38 XEQ 00 39 RCL O 40 X#Y? 41 GTO 04 42 RCL M 43 RCL 00 44 + 45 RCL N 46 1 47 + 48 XEQ 00 49 RCL O 50 X#Y? 51 GTO 04 52 RCL 00 53 STO M 54 ENTER^ 55 X^2 56 STO N 57 * 58 GTO 01 59 LBL 00 60 E5 61 / 62 + |
63 RCL 00
64 0 65 LBL 09 66 RCL IND Z 67 + 68 DSE Z 69 "" 70 DSE Y 71 GTO 09 72 RTN 73 LBL 10 74 ENTER^ 75 CLX 76 LBL 12 77 RCL IND Y 78 + 79 DSE Y 80 "" 81 DSE M 82 GTO 12 83 RTN 84 LBL 01 85 XEQ 10 86 RCL O 87 X#Y? 88 GTO 04 89 RCL 00 90 STO M 91 R^ 92 DSE N 93 GTO 01 |
94 X<>Y 95 ENTER^ 96 X^2 97 STO N 98 ST* Y 99 E5 100 / 101 + 102 STO P 103 LBL 02 104 XEQ 10 105 RCL O 106 X#Y? 107 GTO 04 108 RCL 00 109 STO M 110 RCL P 111 1 112 - 113 STO P 114 DSE N 115 GTO 02 116 X<>Y 117 STO N 118 X^2 119 LASTX 120 ST* Y 121 E5 122 / 123 + 124 STO P |
125 LBL 03 126 XEQ 10 127 RCL O 128 X#Y? 129 GTO 04 130 RCL 00 131 STO M 132 RCL P 133 1 134 - 135 STO P 136 DSE N 137 GTO 03 138 RCL 00 139 ST+ Y 140 STO N 141 X^2 142 - 143 STO P 144 INT 145 X>0? 146 GTO 03 147 RCL O 148 "YES" 149 GTO 05 150 LBL 04 151 "NO" 152 LBL 05 153 AVIEW 154 END |
( 249 bytes / SIZE 1+n^3 )
STACK | INPUTS | OUTPUTS |
X | n | Magic Cste* |
* If the HP-41 displays YES at the end
Example: After storing the following numbers into R01 thru R27
R01 R04 R07
01 23 18
R02 R05 R08 = 17
03 22
R03 R06 R09
24 16 02
R10 R13 R16 15
07 20
R11 R14 R17 = 19 14
09
R12 R15 R18 08
21 13
R19 R22 R25 26
12 04
R20 R23 R26 = 06 25
11
R21 R24 R27 10
05 27
3 XEQ "MGCB?" >>>> "YES" ---Execution time = 35s---
and X = magic constant = 42
Note:
-This version cannot work if n > 6 - not enough registers !
-The following variant overcomes this limitation by calling a subroutine
that calculates the required elements ai,j,k.
Data Registers: R00 = n • R08 = subroutine name ( Register R08 is to be initialized before executing "MGCB?" )
R01 = Magic Constant , R02 thru R07: temp
Flags: /
Subroutine: A program that takes a positive
integer m in X-register & in R03 and returns the m-th element
ai,j,k of the cube.
-Lines 31-46-61 are three-byte GTO 05
01 LBL "MGCB?" 02 STO 00 03 3 04 Y^X 05 STO 06 06 RCL 00 07 X^2 08 LASTX 09 + 10 1 11 + 12 STO 04 13 + 14 STO 03 15 XEQ 00 16 STO 01 17 RCL 06 18 RCL 00 19 X^2 20 LASTX 21 1 22 - 23 ST- Z 24 + 25 STO 04 26 + 27 STO 03 28 XEQ 00 29 RCL 01 30 X#Y? |
31 GTO 05 32 RCL 06 33 RCL 00 34 X^2 35 1 36 - 37 ST- Y 38 RCL 00 39 - 40 STO 04 41 + 42 STO 03 43 XEQ 00 44 RCL 01 45 X#Y? 46 GTO 05 47 RCL 06 48 RCL 00 49 X^2 50 LASTX 51 - 52 ST- Y 53 1 54 + 55 STO 04 56 + 57 STO 03 58 XEQ 00 59 RCL 01 60 X#Y? |
61 GTO 05 62 RCL 00 63 ENTER^ 64 X^2 65 STO 06 66 * 67 1 68 STO 04 69 + 70 STO 03 71 GTO 02 72 LBL 00 73 RCL 00 74 STO 02 75 CLX 76 STO 05 77 LBL 01 78 RCL 03 79 RCL 04 80 - 81 STO 03 82 XEQ IND 08 83 ST+ 05 84 DSE 02 85 GTO 01 86 RCL 05 87 RTN 88 LBL 02 89 XEQ 00 90 DSE 06 |
91 GTO 02 92 RCL 00 93 ENTER 94 X^2 95 STO 06 96 ST* Y 97 STO 04 98 + 99 STO 03 100 STO 07 101 LBL 03 102 XEQ 00 103 RCL 01 104 X#Y? 105 GTO 05 106 DSE 07 107 RCL 07 108 STO 03 109 DSE 06 110 GTO 03 111 RCL 00 112 STO 06 113 X^2 114 LASTX 115 ST* Y 116 STO 04 117 + 118 STO 03 119 STO 07 120 LBL 04 |
121 XEQ 00 122 RCL 01 123 X#Y? 124 GTO 05 125 RCL 03 126 DSE 07 127 RCL 07 128 STO 03 129 DSE 06 130 GTO 04 131 RCL 00 132 ST+ Y 133 STO 06 134 X^2 135 - 136 STO 07 137 STO 03 138 DSE Y 139 GTO 04 140 RCL 01 141 "YES" 142 GTO 06 143 LBL 05 144 "NO" 145 LBL 06 146 AVIEW 147 END |
( 206 bytes / SIZE 009 )
STACK | INPUTS | OUTPUTS |
X | n | Magic Cste* |
* If the HP-41 displays YES at the end
Example: You want to check that "MAGIC" really creates a magic cube for n = 3
Add RTN after line 133 and add LBL "T"
after line 09 in "MAGIC"
"T" ASTO 08
SF 03
3 XEQ "MGCB?" >>>> "YES" ---Execution time = 3m18s---
and X = magic constant = 42
Notes:
-Thus you can check if a formula does create an Andrews magic cube.
-This formula is a function that computes the m-th element of a hypermatrix:
ai,j,k = f(i,j,k)
-You can use:
i - 1 = ( m - 1 ) mod n
j - 1 = int [ ( q - 1 ) / n ]
where q = ( m -1 ) mod n2
k - 1 = int [ ( m - 1 ) / n2 ]
-Use a good emulator like V41 in turbo mode for large n-values...
-Even then, it can take several minutes !
5°) 4-Dimensional Magic Cubes
"4DMGC" displays the elements of 4-D magic (hyper-) cubes of odd orders
n > 1
Formula: cf reference [5]
ai,j,k,q = n3 [ ( i - j + k - q + (n-1)/2
) mod n ]
+ n2
[ ( i - j + k + q - (n+3)/2 ) mod n ]
+ n
[ ( i - j - k - q + (n+1)/2 ) mod n ]
+ [ ( i + j + k + q - (n-1)/2 ) mod
n ] + 1
-The magic constant = n ( n4+1 )/2
Data Registers:
R00 = n R01 thru R06: temp
Flags: /
Subroutines: /
01 LBL "4DMGC" 02 STO 00 03 X^2 04 X^2 05 STO 05 06 LBL 01 07 RCL 05 08 1 09 - 10 STO 01 11 RCL 00 12 ST/ 01 13 MOD 14 X<> 01 15 INT 16 STO 02 17 RCL 00 18 ST/ 02 19 MOD |
20 X<> 02 21 INT 22 STO 03 23 RCL 00 24 ST/ 03 25 MOD 26 X<> 03 27 INT 28 STO 04 29 RCL 01 30 RCL 02 31 - 32 RCL 03 33 + 34 RCL 04 35 - 36 RCL 00 37 2 38 / |
39 INT 40 STO 06 41 + 42 RCL 00 43 MOD 44 LASTX 45 * 46 RCL 01 47 RCL 02 48 - 49 RCL 03 50 + 51 RCL 04 52 + 53 RCL 06 54 - 55 RCL 00 56 MOD 57 + |
58 RCL 00 59 * 60 RCL 01 61 RCL 02 62 - 63 RCL 03 64 - 65 RCL 04 66 - 67 RCL 06 68 + 69 1 70 - 71 RCL 00 72 MOD 73 + 74 RCL 00 75 * 76 RCL 01 |
77 RCL 02 78 + 79 RCL 03 80 + 81 RCL 04 82 + 83 RCL 06 84 - 85 4 86 + 87 RCL 00 88 MOD 89 + 90 1 91 + 92 VIEW X 93 DSE 05 94 GTO 01 95 END |
( 114 bytes / SIZE 007 )
STACK | INPUTS | OUTPUTS |
X | n | / |
Where n is an odd integer > 2
Example: 3 XEQ "4DMGC" >>>> 36 23 64 ........................... 18 59 46
The whole hypercube is:
36 23 64
20 70 33
67 30 26
65 34 24
31 21 71
27 68 28
1st layer = cube n°1
22 66 35
72 32 19
29 25 69
74 43 06
40 03 80
09 77 37
04 75 44
81 41 01
38 07 78
2nd layer = cube n°2
45 05 73
02 79 42
76 39 08
13 57 53
63 50 10
47 16 60
54 14 55
11 61 51
58 48 17
3rd layer = cube n°3
56 52 15
49 12 62
18 59 46
-Here the magic constant is 123 and the central element is 41.
-There are 8 great diagonals:
36 + 41 + 46 = 123
67 + 41 + 15 = 123
22 + 41 + 60 = 123
29 + 41 + 53 = 123
35 + 41 + 47 = 123
26 + 41 + 56 = 123
64 + 41 + 18 = 123
69 + 41 + 13 = 123
Note:
-Other formulas are given in reference [5] to create 4D-magic hypercubes
of order n = 4 m & of order n = 4 m + 2.
References:
[1] Pierre Tougne - "Pour la Science" - pages 121 to 127 - Issue#46
- Aout 1981 ( in French )
[2] http://mathworld.wolfram.com/MagicSquare.html
[3] http://mathworld.wolfram.com/MagicCube.html
[4] Marian Trenkler - "An Algorithm for making Magic Cubes"
[5] Marian Trenkler - "An Algorithm for Magic Tesseracts"
[6] W. S. Andrews - "Magic Squares and Cubes"
[7] Harm Derksen, Christian Eggermont, Arno van den Essen - "Multimagic
Squares"
[8] W. H. Benson & O. Jacoby - "Magic Cubes, New Recreations"
- Dover - ISBN 0-486-24140-8
[9] http://www.multimagie.com
>>> "Pour la Science" is the French edition of "Scientific American"
-References [4] [5] [6] [7] may be dowloaded freely !