HP41C Programs

Program Description

The programs generate consecutively concentric magic squares. There is one program, ODD, for odd order squares, (n = 1, 3, 5, 7, ... ), and one program, EVEN, for even order squares, (n = 4, 6, 8, ... ).

The programs are designed to compute one cell number at a time, given the row and column. A (row, column) is entered to calculate any cell number or, optionally, the square can be stepped through, computing one cell at a time, by repeatedly pressing the R/S button.

Warning: The validity of user input, (n, row, column), is not checked by the programs.

Accessories

HP41C requires 1 memory module for program EVEN.

Registers, Flags

```               ODD                                  EVEN
-----------------------              -------------------------
DATA REGISTERS        FLAGS           DATA REGISTERS         FLAGS
-------------------   -----------     -------------------   ------------
00   n               00  used         00   n                00  used
01   (n+1)/2         01  user         01   (n+1)/2          01  user
02   (n²+1)/2        02  user         02   (n²+1)/2         02  user
03   rrel or crel                     03   rrel or crel     03  used
04   crel or rrel                     04   *(see below)     04  used
05   row                              05   row
06   column          29  cleared      06   column           29  cleared
07   n+1

* EVEN dreg 04: crel+rrel, crel-2rel, ±rrel, ±crel, 2crel, 2rrel, or 0

For ODD:  rrel =  row - (n+1)/2,  crel =  column - (n+1)/2.
For EVEN: rrel = |row - (n+1)/2|, crel = |column - (n+1)/2|.
```

User Instructions

1. load program ODD or EVEN, (or both)
2. [CF][0][1] to step through the square one cell at a time, or,
[SF][0][1] to enter (row, col) for each calculation
3. [CF][0][2] to display actual numbers, or,
[SF][0][2] to display bones numbers
4. [XEQ][ALPHA]ODD[ALPHA] or [XEQ][ALPHA]EVEN[ALPHA]
start of loop
5. at the prompt, key in the order of square, n, and press R/S
start of loop
6. at the prompt, key in the row, and press R/S
7. at the prompt, key in the col, and press R/S
start of loop
(observe display)
8. press [R/S]
end of loops

Note: Flags 01 and 02 can also be keyed as [∑+] and [1/x], for example, [CF][∑+] is equivalent to [CF][0][1].

Examples

Example 1: Step through order 4 square

```               INPUT                    OUTPUT
---------------------------    ------------------
[CF][0][1]
[CF][0][2]
[XEQ][ALPHA]EVEN[ALPHA]            N=?
4 [R/S]                    ROW=?
1 [R/S]                    COLUMN=?
1 [R/S]                    1,1=15
[R/S]                     1,2=4
[R/S]                     1,3=1
[R/S]                     1,4=14
[R/S]                     2,1=6
[R/S]                     2,2=9
[R/S]                     2,3=12
[R/S]                     2,4=7
[R/S]                     3,1=10
[R/S]                     3,2=5
[R/S]                     3,3=8
[R/S]                     3,4=11
[R/S]                     4,1=3
[R/S]                     4,2=16
[R/S]                     4,3=13
[R/S]                     4,4=2
[R/S]                     SQUARE END
[R/S]                     N=?
```

Example 2: Compute (some of) order 5 bones

```               INPUT                    OUTPUT
---------------------------    ------------------
[SF][0][1]
[SF][0][2]
[XEQ][ALPHA]ODD[ALPHA]            N=?
5 [R/S]                    ROW=?
1 [R/S]                    COLUMN=?
1 [R/S]                    1,1=9
[R/S]                     ROW=?
1 [R/S]                    COLUMN=?
5 [R/S]                    1,5=7
[R/S]                     ROW=?
5 [R/S]                    COLUMN=?
5 [R/S]                    5,5=-9
[R/S]                     ROW=?
5 [R/S]                    COLUMN=?
1 [R/S]                    5,1=-7
[R/S]                     ROW=?
3 [R/S]                    COLUMN=?
3 [R/S]                    3,3=0
...
```

Program Listings

Program Listing - ODD

```
----    ----------   --------------     ----    ----------   --------------

01     LBL "ODD"                        71     LBL F        ENE to ESE,WNW to WSW
02     "N=?"                            72     RCL 04
03     PROMPT                           73     X<> 03
04     FIX 0                            74     STO 04
05     CF 29                            75     X>0?
06     STO 00       n                   76     GTO c        ESE,WSW
07     STO 01                           77     0
08     X²                               78     GTO d        ENE,WNW,W,E
09     STO 02                           79     LBL a        NNW,SSW,NE,SW
10     1                                80     -1
11     ST+ 01                           81     GTO d
12     ST+ 02                           82     LBL b        NNE,SSE,NW,SE
13     2                                83     1
14     ST/ 01       (n+1)/2             84     GTO d
15     ST/ 02       (n²+1)/2            85     LBL c        ESE,WSW,N,S
16     LBL 00       input row,col       86     2
17     "ROW=?"                          87     FS?C 00
18     PROMPT                           88     GTO d
19     "COLUMN=?"                       89     SF 00
20     PROMPT                           90     LBL d        2x² + 2y + c
21     LBL 01       calculate           91     RCL 03
22     CF 00                            92     X²
23     STO 06       col                 93     RCL 04
24     RCL 01                           94     +
25     -                                95     ENTER↑
26     STO 04       crel                96     +
27     X<>Y                             97     +
28     STO 05       row                 98     FS? 00
29     RCL 01                           99     CHS
30     -                               100     FS? 02
31     STO 03       rrel               101     GTO 02       display bones
32     X=Y?                            102     RCL 02
33     GTO A        NW-SE              103     +            add (n²+1)/2
34     X>Y?                            104     LBL 02
35     SF 00        below NW-SE        105     CLA
36     X<>Y                            106     ARCL 05
37     X=0?                            107     ","
38     GTO c        N-S                108     ARCL 06
39     X>0?                            109     "="
40     GTO B        E side             110     ARCL X
41     FS? 00                          111     AVIEW        row,col=number
42     GTO C        WNW to SSW         112     RTN
43     GTO a        NNW                113     FS? 01
44     LBL A        NW-SE              114     GTO 00       input row,col
45     0                               115     RCL 00
46     X<> 04                          116     RCL 06
47     X=0?                            117     1
48     GTO d        center             118     +
49     X>0?                            119     X>Y?
50     SF 00        SE                 120     GTO 03
51     GTO b        NW,SE              121     RCL 05
52     LBL B        E side             122     X<>Y
53     FS?C 00                         123     GTO 01       calc row,col+1
54     GTO b        SSE                124     LBL 03
55     LBL C        not NNW or SSE     125     RCL 00
56     +                               126     RCL 05
57     X=0?                            127     1
58     GTO D        NE,SW              128     +
59     X>0?                            129     X>Y?
60     GTO E        below NE-SW        130     GTO 04
61     FS? 00                          131     1
62     GTO F        WNW to WSW         132     GTO 01       calc row+1,1
63     SF 00                           133     LBL 04
64     GTO b        NNE                134     "SQUARE END"
65     LBL D        NE,SW              135     AVIEW
66     STO 04                          136     END
67     GTO a        NE,SW
68     LBL E        below NE-SW
69     FS? 00
70     GTO a        SSW

```

Program Listing - EVEN

```STEP    KEY ENTRY       COMMENTS        STEP    KEY ENTRY       COMMENTS
----    ----------   --------------     ----    ----------   --------------

01     LBL "EVEN"                      151     STO 04
02     "N=?"                           152     GTO 09       NE-SW
03     PROMPT                          153     LBL 07       below NE-SW
04     FIX 0                           154     FS? 00
05     FS? 02                          155     GTO B        SSW
06     FIX 1                           156     LBL 08       WNW,ENE,WSW,ESE
07     CF 29                           157     RCL 03
08     STO 00       n                  158     X<> 04
09     STO 01                          159     STO 03
10     STO 07                          160     RCL 05
11     X²                              161     RCL 01
12     STO 02                          162     X≤Y?
13     1                               163     GTO F        WSW,ESE
14     ST+ 01                          164     RCL 03
15     ST+ 02                          165     1.5
16     ST+ 07       n+1                166     X≠Y?
17     2                               167     GTO 02       not 4 border
18     ST/ 01       (n+1)/2            168     -1.5
19     ST/ 02       (n²+1)/2           169     FS? 00
20     LBL 00       input row,col      170     -2.5
21     "ROW=?"                         171     GTO 03       4 border
22     PROMPT                          172     LBL 02       WNW,ENE: not 4
23     "COLUMN=?"                      173     RCL 04
24     PROMPT                          174     .5
25     LBL 01                          175     X=Y?
26     CF 00                           176     GTO b        W-N,E-N
27     CF 03                           177     2.5
28     CF 04                           178     RCL 04
29     STO 06                          179     X=Y?
30     RCL 01                          180     GTO e
31     -                               181     3.5
32     ABS                             182     X=Y?
33     STO 04       crel               183     GTO e
34     RDN                             184     RCL 04
35     STO 05                          185     XEQ H        cond. SF 03
36     RCL 01                          186     GTO 10       rest of WNW,ENE
37     -                               187     LBL b        W-N,E-N
38     ABS                             188     RCL 03
39     STO 03       rrel               189     XEQ H        cond. SF 03
40     RCL 05                          190     RCL 03
41     RCL 06                          191     CHS
42     X=Y?                            192     STO 04       -crel
43     GTO A        NW-SE              193     GTO 10       W-N,E-N
44     X≤Y?                            194     LBL F        WSW,ESE
45     SF 00        below NW-SE        195     RCL 03
46     RCL 01                          196     1.5
47     X≤Y?                            197     X≠Y?
48     GTO 04       E side             198     GTO 02       not 4 border
49     FS? 00                          199     2.5
50     GTO 05       not NNW            200     FS? 00
51     LBL B        NNW,SSW            201     1.5
52     RCL 03                          202     GTO 03       4 border
53     1.5                             203     LBL 02       WSW,ESE: not 4
54     X≠Y?                            204     RCL 04
55     GTO 02                          205     .5
56     -4.5                            206     X=Y?
57     FS? 00                          207     GTO c        W-S,E-S
58     7.5                             208     RCL 04
59     GTO 03       4 border           209     1.5
60     LBL 02       NNW,SSW: not 4     210     X=Y?
61     RCL 04                          211     SF 03
62     XEQ H        cond. SF 03        212     SF 04
63     RCL 03                          213     XEQ e
64     ENTER↑                          214     -1
65     +                               215     ST* 04
66     RCL 04                          216     GTO 10       WSW,ESE: not 4
67     -                               217     LBL c        W-S,E-S
68     STO 04       2rrel-crel         218     RCL 03
69     GTO 10       NNW,SSW: not 4     219     XEQ E
70     LBL H      | cond. SF 03        220     X=0?
71     XEQ E      |                    221     GTO d        even W-S,E-S
72     X=0?       |                    222     RCL 03
73     SF 03      |                    223     STO 04       crel
74     RTN        |                    224     3.5
75     LBL E      | (INT)x MOD 2       225     X=Y?
76     INT        |                    226     SF 03
77     2          |                    227     GTO 10       odd W-S,E-S
78     MOD        |                    228     LBL d
79     RTN        |                    229     RCL 03
80     LBL A        NW-SE              230     ENTER↑
81     RCL 01                          231     +
82     X<>Y                            232     CHS
83     X>Y?                            233     STO 04       -2crel or -2rrel
84     SF 03        SE                 234     LBL 09
85     RCL 03                          235     0
86     1.5                             236     GTO 14
87     X=Y?                            237     LBL e
88     SF 04                           238     RCL 03
89     0                               239     RCL 04
90     FS? 04                          240     +
91     -2                              241     FS? 04
92     STO 04                          242     XEQ H        cond. SF 03
93     GTO 09       NW-SE              243     FS? 04
94     LBL 04       E side             244     RTN
95     FS? 00                          245     XEQ E
96     GTO C        SSE                246     X≠0
97     LBL 05       not(NNW,SSE,NW-SE) 247     SF 03
98     RCL 05                          248     LBL 10
99     RCL 06                          249     .5
100     +                               250     LBL 14       2x² - y + c
101     RCL 07                          251     RCL 03
102     X=Y?                            252     X²
103     GTO D        NE-SW              253     ENTER↑
104     X≤Y?                            254     +
105     GTO 07       below NE-SW        255     RCL 04
106     FS? 00                          256     -
107     GTO 08       WNW,WSW            257     +
108     LBL C        NNE,SSE            258     FS? 03
109     FC? 00                          259     CHS
110     GTO 02                          260     FS? 00
111     RCL 03                          261     CHS
112     1.5                             262     LBL 03       4x4 border
113     X≠Y?                            263     FS? 02
114     GTO 02                          264     GTO 15       bones
115     4.5                             265     RCL 02
116     GTO 03       4 border           266     +            add (n²+1)/2
117     LBL 02       other NNE,SSE      267     LBL 15
118     RCL 04                          268     CLA
119     .5                              269     ARCL 05
120     X=Y?                            270     ","
121     GTO a        N-E,S-E            271     ARCL 06
122     RCL 04                          272     "="
123     XEQ E                           273     ARCL X
124     X≠0                             274     AVIEW        row,col=number
125     SF 03                           275     RTN
126     RCL 03                          276     FS? 01
127     RCL 04                          277     GTO 00       input row,col
128     +                               278     RCL 00
129     CHS                             279     RCL 06
130     STO 04       -(rrel+crel)       280     1
131     GTO 09       not (N-E,S-E)      281     +
132     LBL a        N-E,S-E            282     X>Y?
133     SF 03                           283     GTO 16
134     RCL 03                          284     RCL 05
135     XEQ E                           285     X<>Y
136     X≠0                             286     GTO 01       calc row,col+1
137     GTO d        rrel even          287     LBL 16
138     RCL 03                          288     RCL 00
139     RCL 04                          289     RCL 05
140     -                               290     1
141     STO 04       rrel-crel          291     +
142     GTO 09       rrel odd           292     X>Y?
143     LBL D        NE-SW              293     GTO 17
144     RCL 03                          294     1
145     .5                              295     GTO 01       calc row+1,1
146     X=Y?                            296     LBL 17
147     SF 04                           297     "SQUARE END"
148     -1                              298     AVIEW
149     FS? 04                          299     END
150     -3

```