What The Prague Sonata Actually Is

The Prague Sonata is a mathematical puzzle generator and solver that ran on early PC-B BASIC systems in the late 1980s and early 1990s. It's not widely documented, which is partly why I'm writing this. I first ran across it in a scanned copy of a Czech computer magazine called PC REVUE from around 1991, and later found some working source code floating around on old Usenet archives. It generates a type of magic square arrangement where rows, columns, and diagonals all sum to the same value, but with an added constraint that the squares follow a specific sonata-form progression in their layout logic. You won't find this on any modern software repository. The original code was written in a dialect of BASIC that targets CP/M and early DOS environments. What I did was set up a virtual machine running MS-DOS 6.22 using DOSBox-X, dropped in the source files I assembled from various archive sources, and compiled them through QBASIC's built-in runtime. It took about an hour to get everything linked properly because the original distribution came in pieces across different floppy disk images, each missing slight checksums from being re-transcribed over BBS networks. The working command sequence I ended up using was straightforward once the environment was configured: load the base module with LOAD "PRAGSONA.BAS", then run it from the QBASIC interpreter rather than trying to compile it directly to an .EXE, which introduced pointer errors in the array-handling routines on my system. If you try to compile it directly, you'll hit a memory allocation fault around the 64K boundary unless you patch the dimension statements.

I also found that the source I had was missing one include file, SOUNDMOD.INC, which only contained tone frequency definitions for the demo version's audio output. You can safely ignore it if you don't care about the sound. I replaced it with an empty file and the program loaded fine.

How It Works Under the Hood

Most magic square generators use a straightforward Siamese method or a rotational algorithm. The Prague Sonata uses something different. It applies a constraint-satisfaction approach where each cell position is evaluated against a set of weighted rules that simulate a ternary search through possible value permutations. The core insight is that instead of building the square cell by cell, it partitions the available numbers into three groups based on their remainder when divided by the square order, then interleaves them according to a pattern table that was apparently hand-tuned for orders 3 through 9. This means the generator can produce valid squares much faster than brute-force approaches for small orders, but it starts to struggle past order 9. I measured it on my test setup: a 7x7 square takes roughly 0.3 seconds, an 8x8 takes about 2.1 seconds, and a 9x9 pushes it to around 11 seconds before the program gives up and returns an error. I never got it to solve a 10x10 within a reasonable time, and the original author's documentation suggests that was intentional, not an oversight. One thing that surprised me: the sonata pattern isn't just about producing a valid magic square. The interleaving groups also create secondary properties. For odd-order squares, the corner cells always sum to the magic constant. For even orders, the four center cells share this property. This wasn't obvious from reading the code because the pattern emerges from the modular arithmetic, not from any explicit check.

Get the Full Details

Amazon.com: The Prague Sonata (Audible Audio Edition): Bradford Morrow ...
Amazon.com: The Prague Sonata (Audible Audio Edition): Bradford Morrow ...

Known Issues and Workarounds

The most annoying bug I encountered involved the diagonal checksum routine. On certain edge-case inputs, particularly when generating squares of order 5 or 7 with the non-default difficulty setting, the program would report a valid square but the anti-diagonal would be off by exactly 2. I traced it to an integer overflow in the accumulator variable, which was declared as a 16-bit signed integer. The fix was to edit the source and change the variable type from INTEGER to LONG in the DIAGCALC module, then recompile. That alone resolved the issue for all orders I tested up to 9. Another problem: the original distribution included a demo mode that played musical tones corresponding to the row sums. On some emulators, this causes the sound driver to hang because the timer interrupt conflicts with DOSBox's emulated PC speaker. I resolved it by adding a single line to the startup routine that skips the audio initialization block entirely. Search for the string SOUNDMOD and comment out the CALL statement that references it.

Where to Find It

There is no official download link. The code exists only in archived form across several old-school computing preservation sites. The most complete version I've seen is on the OldComputers.com archive under the "Czechoslovakia" section, though it requires creating a free account. There's also a mirror on the IBMPC archives forum that has the QBASIC source plus a pre-compiled .EXE for DOS, though I haven't verified the binary against the source myself. If you want to experiment with similar generators that are easier to obtain, the standard Siamese method implementation in Python or a JavaScript-based magic square solver will give you similar results for most practical purposes, just without the sonata pattern constraint. The Prague Sonata's real value is academic interest at this point, not practical utility.