4N 魔方陣

December 12, 2021

4N 魔方陣將 1 到 n 的數字排列在 n x n 的方陣,n 限定為 4 的倍數,也稱這種方陣為雙偶數(evenly-even)方陣,各行、各列與各對角線的和必須相同。

解法思路

4 x 4 方陣可以用兩個方陣來解,其中一個從上而下、由左而右,由 1 依序填寫,不過對角線不填數字,另一個反序由 16 開始填,而且只有對角線才填數字,將兩個方陣合起來就是解答了:

4N 魔方陣

如果 N 大於 2,以 4 X 4 為單位畫對角線:

4N 魔方陣

至於對角線的位置該如何判斷,有兩個公式,如下所示:
左上至右下:j % 4 == i % 4
右上至左下:(j % 4 + i) % 4 == 1

程式實作

#include <stdio.h> 
#include <stdlib.h> 

#define N 8 

int main(void) { 
    int square[N+1][N+1] = {0}; 

    int i, j; 
    for(j = 1; j <= N; j++) { 
        for(i = 1; i <= N; i++){ 
            if(j % 4 == i % 4 || (j % 4 + i) % 4 == 1) 
                square[i][j] = (N+1-i) * N -j + 1; 
            else 
                square[i][j] = (i - 1) * N + j; 
        } 
    } 

    for(i = 1; i <= N; i++) { 
        for(j = 1; j <= N; j++) 
            printf("%2d ", square[i][j]); 
        printf("\n"); 
    } 

    return 0; 
} 
public class Matrix {
    public static int[][] magic(int n) {
        int[][] matrix = new int[n][n]; 

        for(int j = 1; j <= n; j++) { 
            for(int i = 1; i <= n; i++){ 
                if(j % 4 == i % 4 || (j % 4 + i) % 4 == 1) 
                    matrix[i - 1][j - 1] = (n+1-i) * n - j + 1; 
                else 
                    matrix[i - 1][j - 1] = (i - 1) * n + j; 
            } 
        } 
        
        return matrix;
    }
   
    public static void main(String[] args) {
        for(int[] row : Matrix.magic(8)) {
            for(int number: row) {
                System.out.printf("%2d ", number);
            }
            System.out.println();
         }
    }
}
def magic(n):
    matrix = []
    for i in range(n):
        matrix.append([0] * n)
    for j in range(1, n + 1):
        for i in range(1, n + 1):
            if j % 4 == i % 4 or (j % 4 + i) % 4 == 1:
                matrix[i - 1][j - 1] = (n + 1 - i) * n - j + 1
            else:
                matrix[i - 1][j - 1] = (i - 1) * n + j

    return matrix

matrix = magic(8)
print(matrix)
object Matrix {
    def magic(n: Int) = {
        val matrix = new Array[Array[Int]](n, n)

        for(j <- 1 to n; i <- 1 to n) {
            matrix(i - 1)(j - 1) = if(j % 4 == i % 4 || (j % 4 + i) % 4 == 1)
                                       (n+1-i) * n -j + 1
                                   else 
                                       (i - 1) * n + j
        }
        
        matrix
    }
}

Matrix.magic(8).foreach(row => {
    row.foreach(number => printf("%2d ", number))
    println()
})
def magic(n)
    matrix = Array.new(n) {
        Array.new(n, 0)
    }
    1.upto(n) { |j|
        1.upto(n) { |i|
            if j % 4 == i % 4 || (j % 4 + i) % 4 == 1
                matrix[i - 1][j - 1] = (n + 1 - i) * n - j + 1
            else
                matrix[i - 1][j - 1] = (i - 1) * n + j
            end
        }
    }
    
    matrix
end

matrix = magic(8)
p matrix
function magic(n) {
    let matrix = [];
    for(let i = 0; i < n; i++) {
        matrix.push([]);
    }
    
    for(let j = 1; j < n + 1; j++) {
        for(let i = 1; i < n + 1; i++) {
            if(j % 4 == i % 4 || (j % 4 + i) % 4 == 1) {
                matrix[i - 1][j - 1] = (n + 1 - i) * n - j + 1;
            }
            else {
                matrix[i - 1][j - 1] = (i - 1) * n + j;
            }
        }
    }

    return matrix;
}

matrix = magic(8)
console.log(matrix)
magic n =
    [[elem (i + 1) (j + 1) n | j <- [0..n - 1]] | i <- [0..n - 1]]
    where elem i j n = 
            if j `mod` 4 == i `mod` 4 || (j `mod` 4 + i) `mod` 4 == 1
            then (n + 1 - i) * n - j + 1
            else (i - 1) * n + j

main = print $ magic 8

分享到 LinkedIn 分享到 Facebook 分享到 Twitter