--- title: "Magic Square (Syntax)" id: 76510 type: "computer_media" slug: "magic-square-syntax" url: "http://localhost/computer_media/magic-square-syntax/" markdown_url: "http://localhost/computer_media/magic-square-syntax.md" published_at: "2026-11-11T08:02:35+00:00" modified_at: "2026-11-11T08:02:35+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2026/11/magic-square-syntax-1.png" excerpt: "Make a magic square for any number from 35 to 99: every row, column and diagonal adds up to your number." post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" - name: "ZX81" slug: "zx81" taxonomy: "post_tag" url: "http://localhost/tag/zx81/" model: - name: "Sinclair ZX81" slug: "zx81" taxonomy: "model" url: "http://localhost/model/zx81/" - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" indiv: - name: "Eric Hammond" slug: "eric-hammond" taxonomy: "indiv" url: "http://localhost/indiv/eric-hammond/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_type: "Program" programmers: - name: "Eric Hammond" slug: "eric-hammond" taxonomy: "indiv" url: "http://localhost/indiv/eric-hammond/" download_url: "https://archive.org/download/timex-sinclair-software-archive/Magic%20Square%20%281984%29%28Hammond%2C%20Eric%29%28TS1000%29%28US%29%28Program%29.zip" mediadate: "1984" images: - url: "http://localhost/wp-content/uploads/2026/11/magic-square-syntax-1.png" - url: "http://localhost/wp-content/uploads/2026/11/magic-square-syntax-2.png" article_media: - id: 19822 title: "Magic Square" type: "article" url: "http://localhost/article/magic-square/" media_type_tags: "Mathematics" license: false --- # Magic Square (Syntax) Enter a number from 35 to 99, and the program prints a 4 by 4 square of numbers whose rows, columns and diagonals all add up to it. The pattern of the square is held as graphics characters in the REM in line 10; the program reads them back with PEEK and adjusts them for your number, with a random shift so the same number can give different squares. It runs in 1K. The article gives the keystrokes for typing line 10, since its characters are graphics. The printed listing shows a pound sign where the keystrokes say Space: the shifted space is the inverse pound sign, and the download uses it, which is what makes the square come out magic. Published in Syntax May 1984 ([Magic Square](https://timexsinclair.com/article/magic-square/)). ## Source Code ``` 10 REM ▟▘£▛"▒█▝▌▓▀▄▖▙▞▓ 20 PRINT "INPUT A NUMBER BETWEEN 35 AND 99" 30 INPUT A 40 LET A=INT A 50 IF A<35 OR A>99 THEN GOTO 30 60 CLS 70 LET B=INT (RND*10) 80 IF A-22-B*3<1 THEN GOTO 70 90 FOR N=16514 TO 16529 100 LET C=A-149-3*B+PEEK N 110 IF PEEK N<100 THEN LET C=PEEK N+B 120 LET E=N-16514 130 PRINT AT INT (E/4)*5+3,(E-4*INT (E/4)+1)*5;C 140 NEXT N ```