• Stars
    star
    188
  • Rank 199,485 (Top 5 %)
  • Language
    C
  • Created about 3 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Discovering magic squares in Tang Dynasty poems

Observe the above 5x5 matrix of Chinese characters: when read horizontally, it consists of five lines from five different poems, and so does it when read vertically, of the exact same lines. I call it the magic square of Chinese poems. With a brute-force-search over 287,000 lines from the Complete Collection of Tang Dynasty Poems, I discovered 2 such sets. You can reproduce my results by running gcc -O3 mgsq5.c; ./a.out.

風月清江夜
月出夜山深
清夜方歸來
江山歸謝客
夜深來客稀

諸君試觀此二十五言:縱讀之,詩五句也,橫讀之,亦此五句。各出唐人名家之手:風月清江夜,盧照鄰之詩也;月出夜山深,張祜之詩也;清夜、江山、夜深,太白、高適、張籍之詩也。余聞洛出書,有數至於九,縱橫和十五。此非詩中之洛書也歟?故名之曰:唐詩幻方。昔竇滔妻為璇璣圖,蘇伯玉妻為盤中詩。余才不及,遂書程序遍索唐人詩句而得之。反覆賞玩,以為妙絕,乃公諸此,以資眾人一笑耳。

全唐五言合二十八萬七千句,遍歷之,堪為幻方者,唯二。適其一也。其二者,輒語句平淡翻覆,興味不如也:

心如七十人
如何十年間
七十未成事
十年成底事
人間事事慵

首二句,出白樂天,以下出姚合、李昌符、李山甫。此五十字外,五言之幻方不可復得,三言之幻方乃不可勝數。聊揀其一二雅鍊者,以備一觀:

拂秋水
秋夜長
水長東

出任華、白居易、李煜也。

惜春心
春歌斷
心斷絕

出鍾輻、李珣,盧仝也。其餘錄於

七言幻方尤難於五言遠甚。全唐十三萬五千六百句,未得其一焉。故為寬限,但拘四句一三五七字耳。所得遂眾。復欲擇其平仄押韻合於格律,於理通順者,則鮮矣。何耶?使二四句押韻,末句三七字必同韻,犯八病之中小韻之目矣,故唐人不多為也。

七言絕句平起首句不入韻者一:

長江九派人歸少
江 曲 面 年  
九曲寒波不溯流
派 波 知 落
人面不知何處在
歸 溯 處 并
少年流落在并州

縱橫隔行讀之。出皇甫冉、李煜、崔護、薛能。首二句一言長江,一言黃河,似不得法。末二句方點出緣由:胡為兩地尋之而不得其人焉,蓋彼流落并州去也!亦甚滑稽堪為一笑也。

其餘雖間有堪讀者,或病折腰,或病重韻,盡錄於

余所用之程序,當以gcc編譯之如是:

gcc -O3 mgsq5.c -o mgsq5

復運行之,即得五言幻方焉:

./mgsq5

亦可與之以文本路徑、起訖行數也:

./mgsq5 5.txt 0 10000

七言幻方亦同:

gcc -O3 mgsq7.c -o mgsq7;
./mgsq7

然應無所得,蓋不存於全唐詩中也。退而求其前述寬限之格如是:

gcc -O3 mgsq7x.c -o mgsq7x;
./mgsq7x

初,余欲以八后法求唐詩幻方。然其二十五字,每字可填者五千餘,雖復以啟發法為之助,終漫漫不可期其果。細思之,何耶?剪枝苦不早也。或填前字,其於時尚可者,實暗絕後字之路,然程序渾不知,遞歸無功方還。余思治之煩甚,不若易以逐行暴力搜索也。雖云暴力,然亦預存一至四字前綴於哈希集焉。每橫取若干行,乃縱索其五列於表,不存輒立棄之。又其方陣必以對角對稱,若非,亦立棄之。旋以C語言作之,行不逾一時,遂得。

余於算法頗無用意,學詩亦未特工。故所想未必為佳,其法亦未必為速。諸公有妙思者,吾谨願聞焉。

More Repositories

1

shan-shui-inf

Procedurally generated Chinese landscape painting.
HTML
5,464
star
2

fishdraw

procedurally generated fish drawings
JavaScript
2,197
star
3

qiji-font

齊伋體 - typeface from Ming Dynasty woodblock printed books
Python
1,245
star
4

rrpl

Describing Chinese Characters with Recursive Radical Packing Language (RRPL)
JavaScript
846
star
5

linedraw

Convert images to vectorized line drawings for plotters.
Python
744
star
6

wax

A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀
C
730
star
7

q5xjs

A small and fast alternative (experimental) implementation of p5.js
JavaScript
526
star
8

nonflowers

Procedurally generated paintings of nonexistent flowers.
JavaScript
503
star
9

skeleton-tracing

A new algorithm for retrieving topological skeleton as a set of polylines from binary images
C
488
star
10

cope

A modern IDE for writing classical Chinese poetry 格律诗编辑程序
JavaScript
458
star
11

ndwfc

🌊💥 N-dimensional Wave Function Collapse with infinite canvas
JavaScript
308
star
12

psvg

Programmable Scalable Vector Graphics -- drawings that draw themselves
TypeScript
297
star
13

legumes

🎼 A sheet music to polylines renderer
TypeScript
230
star
14

handpose-facemesh-demos

🎥🤟 8 minimalistic templates for tfjs mediapipe handpose and facemesh
JavaScript
185
star
15

Hermit

A man. A horse. A nature.
Python
167
star
16

Processing-Demos-for-The-Pocket-Handbook-of-Image-Processing-Algorithms

Processing Demos made when reading the book *The Pocket Handbook for Image Processing Algorithms in C*
Processing
127
star
17

chinese-hershey-font

Convert Chinese Characters to Single-Line Fonts using Computer Vision
Python
123
star
18

edges2calligraphy

Using pix2pix to convert scribbles to Chinese calligraphy
JavaScript
114
star
19

tk-fangsong-font

剔骨仿宋: Experimental Fang Song style Chinese font
Python
111
star
20

grand-timeline

Interactive grand unified timeline of 30,800 ancient Chinese people / 古人全表
JavaScript
109
star
21

hfmath

Render LaTeX math with Hershey Fonts
TypeScript
91
star
22

wechit

WeChat in Terminal (微信终端版)
Python
88
star
23

VisionOSC

PoseOSC + FaceOSC + HandOSC + OcrOSC + CatOSC + DogOSC
Objective-C++
85
star
24

wasm-fun

Non-trivial programs in hand-written WebAssembly
WebAssembly
77
star
25

PoseOSC

📹🤸‍♂️🤾‍♀️🤺 PoseNet + OSC: send realtime human pose estimation data to your apps
JavaScript
76
star
26

ci-ren

Generative Chinese poetry
Python
74
star
27

r1b

A thermal-printer-oriented, 1-bit graphics rasterizer for 2D and 3D
C
70
star
28

squiggy

vector brushstroke library
TypeScript
63
star
29

asciimare

3D engine powered by ASCII art
Python
61
star
30

Okb.js

Procedural generation toolkit for Javascript - noises, randomness, curves, and more
HTML
58
star
31

ofxPoissonFill

Poisson filling shader for OpenFrameworks
C++
56
star
32

p5-hershey-js

p5.js Hershey Vector Font Library
JavaScript
50
star
33

pmst

🎨 Poor Man's Style Transfer - Painting an image with the style of another, without machine learning
C++
48
star
34

zdic-cli

An offline command-line interface to zdic.net dictionary (漢典)
JavaScript
47
star
35

interesting-polygon-archive

Collection of polygon data in various formats for testing computational geometry algorithms.
Processing
46
star
36

Loshu.js

A linear algebra library for JavaScript 🔢
JavaScript
46
star
37

skeletonization-js

Javascript implementation of image skeletonization
JavaScript
41
star
38

ttf2hershey

Convert True Type Fonts (.ttf) to Hershey vector fonts
Python
40
star
39

fv

An experimental approach to expressing vector math in js (tagged template literals)
JavaScript
30
star
40

t43

A tiny 3D slicer written from scratch
C
30
star
41

LingDong-

Automatically keep my Github profile README updated with a python script and Github Actions
Python
28
star
42

PContour

Processing/Java library for finding contours in binary images
HTML
27
star
43

srcsnap

screenshot-driven version tracking
JavaScript
21
star
44

dbn.js

Recreation of John Maeda's "Design By Numbers" programming environment in JavaScript
JavaScript
17
star
45

TrackpadOSC

💻👋✌️👉Send mac's multitouch trackpad read-out through OSC
Objective-C
17
star
46

xcessing

Friendly Processing-like interface to X11/Xlib in C
C
16
star
47

wax4vscode

Extension for the wax programming language in VS Code (highlight + transpile + run)
TypeScript
15
star
48

svg2pl

convert svg to polylines
C
14
star
49

lbll

tiny experimental language for limited environments
C
14
star
50

fast-many-face-detection-with-cpp-or-openframeworks-on-mac-using-neural-networks

Fast Many Face Detection with C++/OpenFrameworks on macOS using Neural Networks
C++
14
star
51

machining-projection-map

JavaScript
8
star
52

avrlass

AVR Lightweight Assembler (and disassembler)
JavaScript
6
star
53

60-212

JavaScript
5
star
54

teapot.lua

1 path tracer written in pure lua, 1 file, 0 dependencies.
Lua
5
star
55

Hello-World

Lorem Ipsum
3
star
56

cvDictUI

opencv-python tool for generating interactive GUI from any python dictionary
Python
3
star
57

lingdong

LingDong's project links
JavaScript
2
star