Magic Product

# Magic Multiplication for the HP-41

Overview

-This program takes 2 magic squares and calculates a "product" of these matrices that is also a magic square.

Formula:     A is an mxm matrix   B is an nxn matrix  then  C = A*B = [ Ci,j ]  is defined as

Ci,j = n2 A + bi,j 1mxm          where       1mxm  is the mxm matrix whose all elements equal 1

-So,  Ci,j  is also an  mxm matrix  and  C is an mnxmn matrix built with n2 submatrices Ci,j

Program Listing

Data Registers:              R00 = n2               ( Registers Rbb thru Ree & Rbb' thru Ree' are to be initialized before executing "MG*MG" )

•  Rbb .................... •  Ree = A
•  Rbb' ................... •  Ree' = B

>>> When the program stops,   RBB .................... •  REE = C         IF   CF 02

Flag:  F02                        CF 02  <->  the coefficients of C are stored in RBB thru REE
SF 02   <->  the coefficients of C are stored in a DATA file which must be the current file
Subroutines: /

 01  LBL "MG*MG"  02  FS? 02  03  SEEKPT  04  STO O              05  RDN  06  STO N  07  FRC  08  ISG X  09  INT  10  STO 00  11  ST* 00  12  STO Q  13  X<>Y  14  STO M  15  FRC  16  ISG X  17  INT 18  STO P              19  ENTER  20  LBL 01  21  RCL IND M  22  SIGN  23  CLX  24  RCL 00  25  ST* L  26  CLX  27  RCL IND N  28  ST+ L  29  X<> L  30  FC? 02  31  STO IND O  32  FS? 02  33  SAVEX  34  CLX 35  SIGN  36  ST+ M  37  ST+ O  38  RDN  39  DSE P  40  GTO 01  41  R^  42  ST+ N  43  RDN  44  STO P              45  ST- M  46  DSE Q  47  GTO 01  48  ST+ M  49  RCL 00  50  SQRT  51  STO Q 52  ST- N  53  RDN  54  DSE Y  55  GTO 01  56  R^  57  ST+ N  58  RDN  59  STO P              60  STO Y  61  X^2  62  ST- M  63  X<> L  64  DSE Z  65  GTO 01  66  RCL 00  67  SQRT  68  * 69  ENTER  70  X^2  71  CHS  72  RCL O              73  ST+ Y  74  DSE X  75   E3  76  /  77  +  78  X<>Y  79   E5  80  /  81  +  82  CLA  83  END

( 138 bytes / SIZE ??? )

 STACK INPUTS OUTPUTS Z bbb.eee,mm / Y bbb'.eee',nn / X BBB BBB.EEE,NN

with  N = m x n

Example1:    With the following 4x4 magic square

01   14   04   15     store these 16 numbers      R01   R05   R09   R13
08   11   05   10                                              R02   R06   R10   R14
13   02   16   03              in registers                R03   R07   R11   R15            respectively:           control number = 1.01604
12   07   09   06                                              R04   R08   R12   R16

-Let's calculate  A*A,  we can choose  BBB = 17

CF 02  1.01604   ENTER^   ENTER^   17   XEQ "MG*MG"  >>>>   17.27216                ---Eecution time = 2m29s---

-And we get in R17 thru R272 the following magic square

 17 225 65 241 30 238 78 254 20 228 68 244 31 239 79 255 129 177 81 161 142 190 94 174 132 180 84 164 143 191 95 175 209 33 257 49 222 46 270 62 212 36 260 52 223 47 271 63 193 113 145 97 206 126 158 110 196 116 148 100 207 127 159 111 24 232 72 248 27 235 75 251 21 229 69 245 26 234 74 250 136 184 88 168 139 187 91 171 133 181 85 165 138 186 90 170 216 40 264 56 219 43 267 59 213 37 261 53 218 42 266 58 200 120 152 104 203 123 155 107 197 117 149 101 202 122 154 106 29 237 77 253 18 226 66 242 32 240 80 256 19 227 67 243 141 189 93 173 130 178 82 162 144 192 96 176 131 179 83 163 221 45 269 61 210 34 258 50 224 48 272 64 211 35 259 51 205 125 157 109 194 114 146 98 208 128 160 112 195 115 147 99 28 236 76 252 23 231 71 247 25 233 73 249 22 230 70 246 140 188 92 172 135 183 87 167 137 185 89 169 134 182 86 166 220 44 268 60 215 39 263 55 217 41 265 57 214 38 262 54 204 124 156 108 199 119 151 103 201 121 153 105 198 118 150 102

-Here, the magic constant = 2312
-But if you subtract 16 to all these elements, you get a normal magic square, with all the integers between 1 and 256, magic constant = 2056

-Since A is a panmagic square,  C is a panmagic square too !

Example2:    With the same 4x4 matrix as above and the 5x5 matrix below

01   14   22   10   18
07   20   03   11   24
13   21   09   17   05    that we can store, in column order, in  R21 thru R45, control number = 21.04505
19   02   15   23   06
25   08   16   04   12

-The "product" C = A*B is a 20x20 matrix, so there is not enough room in main memory:
-Create a DATA file, size = 401

SF 02
1.01604   ENTER^
21.04505   ENTER^
1         XEQ "MG*MG"  >>>>   1.40020                             ---Execution time = 4m25s---

-And the data file contains the 20x20 magic square:

 26 351 101 376 39 364 114 389 47 372 122 397 35 360 110 385 43 368 118 393 201 276 126 251 214 289 139 264 222 297 147 272 210 285 135 260 218 293 143 268 326 51 401 76 339 64 414 89 347 72 422 97 335 60 410 85 343 68 418 93 301 176 226 151 314 189 239 164 322 197 247 172 310 185 235 160 318 193 243 168 32 357 107 382 45 370 120 395 28 353 103 378 36 361 111 386 49 374 124 399 207 282 132 257 220 295 145 270 203 278 128 253 211 286 136 261 224 299 149 274 332 57 407 82 345 70 420 95 328 53 403 78 336 61 411 86 349 74 424 99 307 182 232 157 320 195 245 170 303 178 228 153 311 186 236 161 324 199 249 174 38 363 113 388 46 371 121 396 34 359 109 384 42 367 117 392 30 355 105 380 213 288 138 263 221 296 146 271 209 284 134 259 217 292 142 267 205 280 130 255 338 63 413 88 346 71 421 96 334 59 409 84 342 67 417 92 330 55 405 80 313 188 238 163 321 196 246 171 309 184 234 159 317 192 242 167 305 180 230 155 44 369 119 394 27 352 102 377 40 365 115 390 48 373 123 398 31 356 106 381 219 294 144 269 202 277 127 252 215 290 140 265 223 298 148 273 206 281 131 256 344 69 419 94 327 52 402 77 340 65 415 90 348 73 423 98 331 56 406 81 319 194 244 169 302 177 227 152 315 190 240 165 323 198 248 173 306 181 231 156 50 375 125 400 33 358 108 383 41 366 116 391 29 354 104 379 37 362 112 387 225 300 150 275 208 283 133 258 216 291 141 266 204 279 129 254 212 287 137 262 350 75 425 100 333 58 408 83 341 66 416 91 329 54 404 79 337 62 412 87 325 200 250 175 308 183 233 158 316 191 241 166 304 179 229 154 312 187 237 162

-Here, the magic constant = 4510
-But if you subtract 25 to all these elements, you get a normal magic square, with all the integers between 1 and 400, magic constant = 4010

-Since A is a panmagic square,  C is a panmagic square too !

-Here is a short routine to visualize the content of a data file:

 01  LBL "VWD"  02  SEEKPT  03   E3  04  *  05  INT  06   E3  07  /  08  SIGN  09  LBL 01  10  GETX  11  VIEW X  12  FC? 21  13  PSE  14  ISG L  15  GTO 01  16  END

-In the example above, key in

1.40020   XEQ "VWD"

or simply   1.400   XEQ "VWD"

Notes:

-"MG*MG"  uses the synthetic registers P & Q
-So, don't interrupt this routine: it would change the content of these tegisters.

-P & Q may also be replaced by R01 & R02 ( in this case, all  bbb > 002 )
-Thus, you could replace line 31 by VIEW X  if  the matrix C has more than 600 elements.
( VIEW X  also changes registers P & Q )

Reference:

[1] Yangkok Kim, Jaechil Yoo  - "An algorithm for constructing magic squares" - Discrete Applied Mathematics 156 (2008) 2804–2809