✨ Magic Square Generator (Odd Order)

Magic-square generator: builds odd-order magic squares where every row, column and diagonal sums to the same constant.

📐 工作原理与说明
幻方阶数 n 限幅 1-31,输入偶数阶时 +1(超过 31 则 −1);暹罗法:起始位置 (0, ⌊n/2⌋),填 1 至 n² 时先试 (r−1+n) % n、(c+1) % n,该格已占用则改落到 (r+1) % n;幻和 = n × (n² + 1) ÷ 2,每行/每列/两条对角线和均等于该值
本工具用于幻方生成器(奇数阶)的实时计算,依据对应公式与标准运算规则处理,结果按精度实时输出;纯前端运行,数据不上传。

📚 深度解析:幻方生成(奇数阶)

💡 常见使用场景

  • 数学游戏:生成奇数阶幻方,各行/列/对角线和相等
  • 教学演示:展示罗伯法(右上走)构造
  • 算法验证:核对幻和 = n(n²+1)/2
方法
用法:输入奇数阶 n,工具用罗伯法生成 n 阶幻方,每行、每列、两对角线数字和均等于幻和 n(n²+1)/2。
算例
例:3 阶幻方(幻和 15):8 1 6 / 3 5 7 / 4 9 2,每行每列两对角均为 15。5 阶幻和 = 5×(25+1)/2 = 65,工具自动生成满足的方阵。

❓ 常见问题(FAQ)

为什么只支持奇数阶?
罗伯法针对奇数阶;偶数阶(双偶/单偶)有不同构造法,本工具聚焦奇数阶最简情形。
幻和公式怎么来?
1~n² 总和 n²(n²+1)/2,均分到 n 行,故每行和 = n(n²+1)/2。

Formula & Standard References

Item Formula / Note Source / Standard
Odd-order construction The Siamese (de la Loubere) method starts at the top middle cell and moves up-right de la Loubere S. (1688); Andrews WS, Magic Squares and Cubes (1908)
Wrap-around rule Moving past the top wraps to the bottom row; past the right edge wraps to the leftmost column Andrews WS, Magic Squares and Cubes (1908)
Occupied-cell rule If the target cell is occupied, place the number directly below the previous cell Andrews WS, Magic Squares and Cubes (1908)
Magic constant For order n, S = n(n^2 + 1)/2; every row, column and both diagonals sum to S Andrews WS, Magic Squares and Cubes (1908)
⚠️ 使用说明与注意事项