finds.dev

LingDong-/magic-square-poems

Discovering magic squares in Tang Dynasty poems

View on GitHub

★ 190Cupdated Mar 2021

// the take

A brute-force search over 287,000 lines of Tang Dynasty poetry to find 'magic square' poems — matrices where the same lines read identically horizontally and vertically. Written entirely in C with no dependencies. This is a research curiosity, not a library or tool you'd integrate into anything.

The core algorithm is clever: rather than backtracking (which the author explicitly tried and abandoned), it precomputes 1–4 character prefix hash sets and prunes column candidates early, making a 287k-line exhaustive search finish in under an hour. The diagonal symmetry optimization halves the search space for free. The C code is lean and does exactly what it needs to do — no framework overhead, no abstraction layers. The README is written in classical Chinese to match the subject matter, which is a delightful bit of craft.

This is a finished experiment, not a maintained project — last commit 2021, 190 stars, no issues, no PRs, no roadmap. The search is hardcoded to the Quan Tangshi corpus bundled as text files; adapting it to any other corpus requires reading the source. There's no documentation in English beyond the opening paragraph, so most developers can't follow the methodology explanation. Seven-character magic squares were never found in the full corpus, and the relaxed variant produces mostly metrically broken results — the problem itself may be exhausted.

We read the README, the directory tree and a key source file in June 2026. Nobody pays to be listed. How we judge →

// at a glance

Stars
190
Forks
15
Language
C
Last commit
26 Mar 2021

// nearby

If magic-square-poems isn't quite it.

All repos →