✨ 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)
⚠️ 使用說明與注意事項