This program generates a 4×4 magic square whose row, column, and diagonal sums equal any number the user inputs. After accepting the number A, it computes a base value B = INT((A−30)/4) and fills a 4×4 grid printed with AT coordinates, using offset arithmetic to place 16 distinct values. Five conditional branches at lines 62–95 handle the four fractional remainders of (A−30)/4 (0, 0.25, 0.5, 0.75, and the special cases A=31 and A=32), adjusting the diagonal entries to maintain the magic property. The program also validates the input range, nudging the user toward numbers between 31 and 54 as most suitable. Output is formatted across rows 5, 7, 9, and 11 at columns 8, 12, 16, and 20 of the display.
Program Analysis
Program Structure
The program is divided into three logical phases: input and validation (lines 10–50), magic square construction and display (lines 55–95), and explanatory output (lines 100–135), followed by a STOP at line 200.
- Input/Validation (10–50): Prompts the user for a number, echoes it, and prints one of five range-commentary messages.
- Base Calculation (55): Computes
B = INT((A-30)/4), which serves as the offset for every cell value. - Grid Display (60–95): Uses
PRINT ATto place 16 numbers in a 4×4 layout at fixed screen coordinates. Fractional-remainder branches fine-tune four of the cells. - Explanation (100–135): Describes the magic square property to the user.
Magic Square Algorithm
The core technique is a parameterised magic square based on a standard 4×4 template. With a chosen number A, the base B = INT((A-30)/4) is used as an additive offset. The 16 cell values are B+0 through B+18 (not all are used; 16 distinct offsets appear), derived so the four rows, four columns, and two main diagonals each sum to A.
The fractional part of (A-30)/4 can be 0, 0.25, 0.5, or 0.75, corresponding to A mod 4 being 2, 3, 0, or 1 respectively. Lines 62–95 handle each case by substituting four specific cells (the right column and one diagonal group at AT rows 5,7,9,11 and columns 8,12,16,20) with values incremented by a fixed amount, preserving the magic sum.
Grid Layout
The 4×4 grid is printed using PRINT AT row,col;value statements spread across lines 60–95. The screen positions used are:
| Row | Col 8 | Col 12 | Col 16 | Col 20 |
|---|---|---|---|---|
| 5 | B+8 | B+5 | B+2 | (conditional) |
| 7 | B+3 | (conditional) | B+9 | B+4 |
| 9 | (conditional) | B | B+7 | B+10 |
| 11 | B+6 | B+11 | (conditional) | B+1 |
The four “conditional” cells (one per row, forming a diagonal and the rightmost column) are overwritten by whichever fractional-remainder branch fires.
Notable Techniques
- Fractional remainder detection: Rather than using MOD arithmetic, the program compares
(A-30)/4againstB+0.25,B+0.5, andB+0.75using equality tests, which works correctly for integer inputs. - Special-case lines 62–63: A=31 and A=32 are handled separately before the general fractional branches. This is likely needed because the general formula produces incorrect or out-of-range cell values near the lower boundary (A close to 30), and these two inputs were corrected by hand.
- Chained
PRINT AT: MultipleATclauses are chained in a singlePRINTstatement, efficiently positioning several values without separatePRINTcalls. - Input nudging: Five messages guide the user toward the 31–54 “sweet spot” where the algorithm is most reliable, though the program still attempts a square for any value.
Potential Bugs and Anomalies
- Line 60 syntax:
PRINT AT 5,8;B+8,AT 5,12;B+5,AT 5,16;B+2uses a comma before the secondAT, which in standard Sinclair BASIC separates print items but should work correctly sinceATis a valid print item separator in this dialect. - Line 65 double semicolon:
PRINT AT 5,20;;B+16contains a redundant double semicolon. The extra semicolon is harmless but unnecessary. - Values near A=30: When A=30,
B=0and(A-30)/4=0, so theB=0branch at line 95 fires, but some cell values become 0 or negative (e.g.,B+0 = 0), which may not form a valid positive magic square. - Lines 62–63 overlap: The general fractional branches at lines 65, 75, 85, and 95 will also fire for A=31 and A=32 (since their remainders match), potentially overwriting the hand-coded corrections from lines 62–63 with the general formula values.
Content
Source Code
5 REM NUMBER
10 PRINT "THINK OF A NUMBER"
20 INPUT A
25 PRINT " YOU SELECTED ";A;"."
30 IF A<15 THEN PRINT "YOUR NUMBER WILL BE USED BUT IT IS TOO SMALL."
35 IF A<31 AND A>14 THEN PRINT "WE WILL ACCEPT YOUR NUMBER BUT WOULD PREFER A LARGER NUMBER."
40 IF A>30 AND A<55 THEN PRINT "THAT IS A REASONABLE NUMBER."
45 IF A>54 AND A<100 THEN PRINT "WE WILL ACCEPT YOUR NUMBER BUT WOULD PREFER A SMALLER NUMBER."
50 IF A>99 THEN PRINT "YOUR NUMBER IS TOO LARGE BUT IT WILL BE USED."
55 LET B=INT ((A-30)/4)
60 PRINT AT 5,8;B+8,AT 5,12;B+5,AT 5,16;B+2
62 IF A=31 THEN PRINT AT 5,20;16,AT 7,12;15,AT 9,8;14,AT 11,16;13
63 IF A=32 THEN PRINT AT 11,16;14,AT 9,8;15,AT 5,20;17,AT 7,12;16
65 IF (A-30)/4=B+.25 THEN PRINT AT 5,20;;B+16,AT 7,12;B+15,AT 9,8;B+14,AT 11,16;B+13
70 PRINT AT 7,8;B+3,AT 7,16;B+9,AT 7,20;B+4
75 IF (A-30)/4=B+.5 THEN PRINT AT 5,20;B+17,AT 7,12;B+16,AT 9,8;B+15,AT 11,16;B+14
80 PRINT AT 9,12;B,AT 9,16;B+7,AT 9,20;B+10
85 IF (A-30)/4=B+.75 THEN PRINT AT 5,20;B+18,AT 7,12;B+17,AT 9,8;B+16,AT 11,16;B+15
90 PRINT AT 11,8;B+6,AT 11,12;B+11,AT 11,20;B+1
95 IF (A-30)/4=B THEN PRINT AT 5,20;B+15,AT 7,12;B+14,AT 9,8;B+13,AT 11,16;B+12
100 PRINT
105 PRINT
110 PRINT "THIS IS A 4 BY 4 MAGIC SQUARE"
115 PRINT "FOR ";A;", THE SUM OF HORIZONTAL"
120 PRINT "LINES OR VERTICAL COLUMNS WILL"
125 PRINT "BE YOUR CHOSEN NUMBER OF ";A;"."
130 PRINT "ALSO TRY THE DIAGONALS AND "
135 PRINT "CORNERS."
200 STOP
300 SAVE "1014%5"
310 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
