# 幻方

幻方是一种数字方阵，其中每一行、每一列以及两条主对角线上的数字之和都等于同一个值，这个值称为幻数（magic constant）。在 n 阶的标准幻方（或称“正规”幻方）中，填入的是从 1 到 n² 的各不相同的整数，幻数为

\[ M_2(n) = \frac{1}{n}\sum_{k=1}^{n^2} k = \frac{n(n^2+1)}{2}. \]

一边上的格子数即为幻方的阶。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 幻数 | 对于 n 阶正规幻方，\( M_2(n) = n(n^2+1)/2 \)；4 阶幻方的幻数为 34。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup> |
| 最小情形 | 2 阶不存在正规幻方；3 阶幻方在旋转和反射意义下是唯一的。<sup>[2](https://oeis.org/A006052)</sup> |
| 计数 | 将旋转和反射视为同一种：1 至 5 阶分别有 1、0、1、880、275,305,224 个幻方；6 阶的数量于 2024 年算出，为 17,753,889,197,660,635,632 个。<sup>[2](https://oeis.org/A006052)</sup> |
| 最早的例子 | 中国古代的 3×3 洛书幻方，附会于大禹与洛水神龟的传说（约公元前 2200—3000 年）；最早的书面记载见于《庄子》，约公元前 300 年。<sup>[3](https://academic.oup.com/book/53801/chapter-abstract/422180671)</sup> |
| 欧洲的里程碑 | 阿尔布雷希特·丢勒 1514 年的版画《忧郁 I》中包含了欧洲最早出现的幻方，是一个嵌入了 1514 年这个年份的 4×4 幻方。<sup>[4](https://mathshistory.st-andrews.ac.uk/Strick/durer.pdf)</sup> |
| 变体 | 半幻方（仅行与列）、泛对角线幻方（所有折断对角线也满足）、双幻方与三幻方（在平方和立方下仍为幻方）、关联幻方（关于中心对称的每对格子之和为 n²+1）。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup> |
| 泛对角线幻方的存在性 | 泛对角线幻方在 2(2n+1) 阶不存在，但在其他所有阶都存在；880 个 4 阶幻方中有 48 个是泛对角线幻方。<sup>[5](https://mathshistory.st-andrews.ac.uk/HistTopics/Mathematical_games/)</sup> |

## 定义与基本算术

定义条件是：全部 n 个行和、全部 n 个列和以及两条主对角线之和都等于同一个常数。对于正规幻方，这个常数可由所有元素之和 \( 1 + 2 + \cdots + n^2 = n^2(n^2+1)/2 \) 除以 n 行得到，即 \( n(n^2+1)/2 \)。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup> 更一般地，若方阵的元素构成从 A 开始、公差为 D 的等差数列，则幻数为

\[ M_2(n; A, D) = \frac{n}{2}\left[2A + D(n^2-1)\right]. \]

用 n²+1 减去每个元素，可将任意幻方变为另一个幻方，即补幻方。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup>

## 变体

定义条件的若干弱化和强化各有其名称。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup>

- **半幻方**满足行和列的条件，但一条或两条主对角线不满足。
- **泛对角线幻方**（或称全幻方、魔鬼幻方）还要求折断对角线也满足条件，即把方阵看作环面上的图形时环绕的对角线。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup><sup> • </sup><sup>[5](https://mathshistory.st-andrews.ac.uk/HistTopics/Mathematical_games/)</sup>
- **双幻方**在将每个元素替换为其平方后仍为幻方；**三幻方**在元素、平方和立方下均保持幻方性质。
- **关联幻方**中关于中心对称的每一对格子之和均为 n²+1。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup>
- 也可以构造在乘法下为幻方的方阵，以及在加法和乘法下均为幻方的方阵。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup>

莱昂哈德·欧拉研究过泛对角线幻方，现已知道 2(2n+1) 阶即 6、10、14 等阶的泛对角线幻方不存在，而其他所有阶都存在。<sup>[5](https://mathshistory.st-andrews.ac.uk/HistTopics/Mathematical_games/)</sup>

## 构造

对于奇数阶，有一种称为暹罗法的简便技巧：将 1 放在顶行中央的格子中，此后每个数都放在上一个数的右上方一格，遇到边缘则环绕，若目标格已被占据则改为向下一格。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup> 1911 年版《大英百科全书》描述了相关的循路径构造法，其中包括让数字 1、2、3 沿国际象棋中马的走法行进，走出方阵时环绕到对侧相应格子的构造。<sup>[6](https://en.wikisource.org/wiki/1911_Encyclop%C3%A6dia_Britannica/Magic_Square)</sup>

偶数阶需要不同的处理方法，现代研究按 n 为奇数、单偶数（偶数但不能被 4 整除）或双偶数（能被 4 整除）来对算法分类。<sup>[7](https://link.springer.com/article/10.1007/s40314-026-03731-3)</sup> 构造也可以表述为在行、列和对角线求和约束下的约束满足问题；这类模型适用于任意阶，但随着 n 增大计算代价急剧上升，因此大幻方采用快速构造方法。<sup>[7](https://link.springer.com/article/10.1007/s40314-026-03731-3)</sup>

## 幻方的计数

确定任意阶幻方的数目仍是一个未解决的问题，但前几个值已精确知道。将旋转和反射下等价的方阵视为同一种，1、2、3、4、5 阶的序列为 1、0、1、880、275,305,224。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup><sup> • </sup><sup>[2](https://oeis.org/A006052)</sup> 880 个 4 阶幻方由贝尔纳·弗雷尼克尔·德·贝西枚举，于 1693 年在他去世后发表；5×5 的计数由 R. 施勒佩尔于 1973 年算出。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup><sup> • </sup><sup>[2](https://oeis.org/A006052)</sup>

6 阶的计数长期无法精确计算。1998 年，K. 平恩和 C. 维切科夫斯基借助统计力学方法进行蒙特卡洛模拟，估计其为 \( (1.7745 \pm 0.0016) \times 10^{19} \)。<sup>[1](https://mathworld.wolfram.com/MagicSquare.html)</sup> 2024 年，蓑英敏计算出了精确值 17,753,889,197,660,635,632，与该估计相符。<sup>[2](https://oeis.org/A006052)</sup>

## 历史

**中国。** 幻方在文字记载的传统中可追溯到古代中国。洛书是一个 3×3 方阵，其背后有一段传说：神话中的大禹看见一只从洛水中浮出的神龟，龟腹甲上的纹路横数、竖数、斜数都得相同的和，这一图案被解读为神秘的征兆。<sup>[3](https://academic.oup.com/book/53801/chapter-abstract/422180671)</sup> 关于幻方的最早书面记载约在公元前 300 年，见于庄子的著作。<sup>[3](https://academic.oup.com/book/53801/chapter-abstract/422180671)</sup> 洛书后来成为风水罗盘派的基础，该派的大师们至今仍使用由它衍生出的 18 至 36 幅标准图。<sup>[8](https://journals.sagepub.com/doi/full/10.1177/2158244015585828)</sup>

**希腊与伊斯兰世界。** 13 世纪末至 14 世纪初的希腊学者曼努埃尔·莫斯霍普洛斯曾论述过这一主题。<sup>[6](https://en.wikisource.org/wiki/1911_Encyclop%C3%A6dia_Britannica/Magic_Square)</sup><sup> • </sup><sup>[15](https://www.encyclopedia.com/science/dictionaries-thesauruses-pictures-and-press-releases/moschopoulos-manuel)</sup> 中世纪伊斯兰世界取得了重大进展，创造并改进了大量构造方法。最初的契机是 9 世纪一篇佚名希腊文本的翻译，该文描述了高度发达的排列方式，被视为古代幻方研究的顶峰。<sup>[9](https://link.springer.com/book/10.1007/978-3-030-17993-9)</sup> 阿拉伯文献中最早提及幻方的是活跃于 9 世纪的阿里·塔巴里的《智慧乐园》。<sup>[10](https://doi.org/10.1163/9789004471481_011)</sup>

**印度。** 幻方在印度已有两千多年的历史；早期出现于与龙树相关的著作中，包括一个幻和为 100、称为“龙树方”的 4×4 泛对角线幻方；伐罗诃密希罗的《广集》（约公元 6 世纪）使用一个 4×4 泛对角线幻方“一切吉祥方”来分配香水配方中的十六种成分。<sup>[11](https://insa.nic.in/writereaddata/UpLoadedFiles/IJHS/01-43539_2024_127_OnlinePDF123-142.pdf)</sup> 耆那教学者塔库拉·费鲁是印度已知第一位在数学著作中处理幻方的人，他按阶对幻方分类并给出了构造方法。<sup>[12](https://vibha.bvmap.org/Content/StudyMaterials/English/Encyclopedia%20of%20Classical%20Indian%20Sciences%20-%20Selin_Narasimha%20%282007%29.pdf)</sup> 梵语传统的集大成之作是纳拉亚纳·潘迪塔的《算法月光》（约 1356 年）中的“吉祥计算”一章，专论幻方及衍生的幻图，并给出了系统的构造规则。<sup>[12](https://vibha.bvmap.org/Content/StudyMaterials/English/Encyclopedia%20of%20Classical%20Indian%20Sciences%20-%20Selin_Narasimha%20%282007%29.pdf)</sup><sup> • </sup><sup>[11](https://insa.nic.in/writereaddata/UpLoadedFiles/IJHS/01-43539_2024_127_OnlinePDF123-142.pdf)</sup>

**欧洲。** 在中世纪和文艺复兴时期的欧洲，幻方被用作行星魔法和灵性魔法的图示以及护身符。<sup>[13](https://theses.newcastle.ac.uk/jspui/bitstream/10443/1318/1/Hasler%2011.pdf)</sup> 16 世纪初，科内利乌斯·阿格里帕构造了 3 至 9 阶的幻方，将它们与当时已知的七颗行星（包括太阳和月亮）相联系。<sup>[5](https://mathshistory.st-andrews.ac.uk/HistTopics/Mathematical_games/)</sup> 欧洲最著名的一次出现是在阿尔布雷希特·丢勒 1514 年的铜版画《忧郁 I》中，其中包含欧洲最早展示的幻方：一个由 1 到 16 组成的 4×4 方阵，行和与列和均为 34，而底行的 15 和 14 两个数字给出了年份 1514。<sup>[4](https://mathshistory.st-andrews.ac.uk/Strick/durer.pdf)</sup>

## 现代地位

幻方曾被当作护身符和占星奇物，如今则作为数论和趣味数学中的问题来研究。<sup>[14](https://www.sciencedirect.com/science/article/pii/016093279590522V)</sup> 它们也出现在应用领域：幻方已被用于密码学、计算机科学和游戏设计，其构造也作为约束满足与优化问题来研究。<sup>[7](https://link.springer.com/article/10.1007/s40314-026-03731-3)</sup>

## References

1. [Magic Square, Wolfram MathWorld](https://mathworld.wolfram.com/MagicSquare.html)
2. [A006052: Number of magic squares of order n, OEIS](https://oeis.org/A006052)
3. [Seymour S. Block and Santiago A. Tavares, "History of Magic Squares," in *Before Sudoku: The World of Magic Squares*, Oxford University Press, 2009](https://academic.oup.com/book/53801/chapter-abstract/422180671)
4. [Albrecht Dürer, MacTutor History of Mathematics (Strick biographical poster)](https://mathshistory.st-andrews.ac.uk/Strick/durer.pdf)
5. [Mathematical games, MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk/HistTopics/Mathematical_games/)
6. ["Magic Square," 1911 Encyclopædia Britannica, Wikisource](https://en.wikisource.org/wiki/1911_Encyclop%C3%A6dia_Britannica/Magic_Square)
7. ["Constructing magic squares: an integer constraint satisfaction problem and a fast approach," Computational and Applied Mathematics, 2026](https://link.springer.com/article/10.1007/s40314-026-03731-3)
8. [Albert Ting Pat So, Eric Lee, Kin Lun Li, and Dickson Koon Sing Leung, "Luo Shu: Ancient Chinese Magic Square on Linear Algebra," SAGE Open, 2015](https://journals.sagepub.com/doi/full/10.1177/2158244015585828)
9. [Jacques Sesiano, *Magic Squares: Their History and Construction from Ancient Times to AD 1600*, Springer, 2019](https://link.springer.com/book/10.1007/978-3-030-17993-9)
10. ["Amulets and Talismans in the Earliest Works of the Corpus Bunianum," in *Amulets and Talismans of the Middle East and North Africa in Context*, Brill](https://doi.org/10.1163/9789004471481_011)
11. [Mandadi Varuneshwar Reddy, "Pandiagonal magic squares in the Bhadragaṇita of Gaṇitakaumudī," Indian Journal of History of Science 59 (2024): 123–142](https://insa.nic.in/writereaddata/UpLoadedFiles/IJHS/01-43539_2024_127_OnlinePDF123-142.pdf)
12. [Encyclopedia of Classical Indian Sciences (Selin, ed., 2007), chapter on magic squares](https://vibha.bvmap.org/Content/StudyMaterials/English/Encyclopedia%20of%20Classical%20Indian%20Sciences%20-%20Selin_Narasimha%20%282007%29.pdf)
13. [Origin of magic squares, PhD thesis, Newcastle University](https://theses.newcastle.ac.uk/jspui/bitstream/10443/1318/1/Hasler%2011.pdf)
14. ["Two thousand years of numerical magic squares," Endeavour / ScienceDirect](https://www.sciencedirect.com/science/article/pii/016093279590522V)
15. [encyclopedia.com](https://www.encyclopedia.com/science/dictionaries-thesauruses-pictures-and-press-releases/moschopoulos-manuel)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Computational and probabilistic number theory › Recreational number theory

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/magic-square

English original: https://www.edgepedia.org/magic-square
