finds.dev← search

// the find

LingDong-/magic-square-poems

★ 190 · C · updated Mar 2021

Discovering magic squares in Tang Dynasty poems

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.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →