유모어

마방진

자유지향 2009. 1. 10. 14:22

마방진(魔方陣, magic square) 

 

楊輝《續古摘奇算經》(1275年)

 

가로, 세로 nxn칸에 1부터 n의 제곱 까지의 자연수열을 한번씩 써 넣어 행과, 열, 대각선의 각 방향의 합이 모두 같도록

만든 정방행렬. 각 줄의 합은 수학적으로 풀어보면 n(n^2+1)/2 가 된다.

 

홀수 방진, 짝수 방진, 대칭 방진, 입체 방진, 소수 마방진 ...

 
 
 

 


712114
213811
163105
96154





더보기


3×3=9
1 + 2 + 3 + ・・・ + 7 + 8 + 9 = 45   45 / 3 = 15

4 3 8
9 5 1
2 7 6






4×4=16
1+2+3+ ・・・ +14+15+16 = 136   136 / 4 = 34

16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

13 3 2 16
8 10 11 5
12 6 7 9
1 15 14 4

14 11 14 5
13 6 3 12
1 10 15 8
16 7 2 9

1 7 14 12
10 16 5 3
15 9 4 6
8 2 11 13

1 2 15 16
13 14 3 4
12 7 10 5
8 11 6 9

12 5 14 3
8 9 2 15
1 16 7 10
13 4 11 6

12 2 15 5
1 7 10 16
13 11 6 4
8 14 3 9

13 3 16 2
8 10 5 11
1 15 4 14
12 6 9 7

1 14 11 8
15 4 5 10
6 9 16 3
12 7 2 13

1 12 13 8
15 6 3 10
4 9 16 5
14 7 2 11

1 15 8 10
14 4 5 11
7 9 16 2
12 6 3 13

3 10 8 13
6 15 1 12
9 4 14 7
16 5 11 2





11 24 7 20 3
4 12 25 8 16
17 5 13 21 9
10 18 1 14 22
23 6 19 2 15

11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

14 25 12 5 9
19 3 11 24 8
18 20 4 6 17
1 15 16 23 10
13 2 22 7 21

1 17 8 24 15
9 25 11 2 18
12 3 19 10 21
20 6 22 13 4
23 14 5 16 7

1 7 13 19 25
14 20 21 2 8
22 3 9 15 16
10 11 17 23 4
18 24 5 6 12


6×6=36
19 5 13 33 18 23
17 7 11 30 15 31
9 28 21 16 8 29
35 27 22 4 20 3
6 10 32 26 36 1
25 34 12 2 14 24

7×7=49
32 40 48 7 8 16 24
38 46 5 13 21 22 30
44 3 11 19 27 35 36
1 9 17 25 33 41 49
14 15 23 31 39 47 6
20 28 29 37 45 4 12
26 34 42 43 2 10 18

8×8=64
1 10 19 28 64 55 46 37
54 45 33 2 11 20 32 63
24 31 62 53 41 34 3 12
35 4 16 23 30 61 49 42
57 50 43 36 8 15 22 29
14 21 25 58 51 44 40 7
48 39 6 13 17 26 59 52
27 60 56 47 38 5 9 18

8×8=64
1 58 3 60 61 6 63 8
16 55 14 53 52 11 50 9
17 42 19 44 45 22 47 24
32 39 30 37 36 27 34 25
40 31 38 29 28 35 26 33
41 18 43 20 21 46 23 48
56 15 54 13 12 51 10 49
57 2 59 4 5 62 7 64

10×10=100
41 73 16 45 77 18 24 99 100 12
87 62 90 19 66 33 38 15 51 44
88 89 13 57 30 61 55 4 82 26
20 65 7 84 59 22 23 56 98 71
52 46 74 40 85 47 29 21 43 68
83 34 39 1 28 76 92 50 27 75
5 8 81 86 2 60 67 93 54 49
14 17 95 32 91 69 70 58 6 53
36 80 48 63 64 25 11 72 9 97
79 31 42 78 3 94 96 37 35 10


10×10=100
18 43 74 99 30 55 6 31 62 87
93 68 49 24 80 5 81 56 37 12
22 47 53 78 9 84 15 40 66 91
97 72 28 3 59 34 90 65 41 16
1 26 32 82 13 38 84 44 75 100
76 51 57 7 63 88 19 69 50 25
85 60 36 11 92 17 98 73 29 4
10 35 61 86 67 42 23 48 54 79
89 64 45 20 21 96 77 52 33 8
14 39 70 95 71 46 2 27 58 83

10×10=100
52 54 41 43 46 45 58 50 57 59
62 34 40 68 36 35 63 61 37 69
89 87 11 13 86 15 18 20 84 82
22 27 30 78 75 76 73 21 74 29
9 94 10 3 95 96 8 1 97 92
99 4 91 93 5 6 98 100 7 2
79 77 80 28 25 26 23 71 24 72
19 17 81 88 16 85 83 90 14 12
32 64 70 38 65 66 33 31 67 39
42 47 51 53 56 55 48 60 44 49

10×10=100
1 92 3 94 5 96 97 8 99 10
20 89 18 87 16 85 84 13 82 11
21 72 23 24 75 76 77 28 79 30
40 69 33 67 36 65 64 38 62 31
41 52 48 54 45 46 57 53 59 50
60 49 58 47 55 56 44 43 42 51
70 39 68 37 66 35 34 63 32 61
71 22 73 74 26 25 27 78 29 80
90 19 88 17 86 15 14 83 12 81
91 2 93 4 95 6 7 98 9 100


변칙마방진

7 2 3

4
5 6 1


 

더보기

완전방진 례, 5×5

9 1 23 20 12
25 17 14 6 3
11 8 5 22 19
2 24 16 13 10
18 15 7 4 21
사선 계산 례
9+3+22+16+15=65
1+25+19+13+7=65
23+17+11+10+4=65
20+14+8+2+21=65
1+14+22+10+18=65
23+6+19+2+15=65
20+3+11+24+7=65
12+25+8+16+4=65
 

 

 

 

 

홀수 마방진

 

C

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

#define N 5

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

i = 0;
j = (N+1) / 2;

for(key = 1; key <= N*N; key++) {
if((key % N) == 1)
i++;
else {
i--;
j++;
}

if(i == 0)
i = N;
if(j > N)
j = 1;

square[i][j] = key;
}

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

return 0;
}

Java

public class Matrix {
public static int[][] magicOdd(int n) {
int[][] square = new int[n+1][n+1];

int i = 0;
int j = (n+1) / 2;

for(int key = 1; key <= n*n; key++) {
if((key % n) == 1)
i++;
else {
i--;
j++;
}

if(i == 0)
i = n;
if(j > n)
j = 1;

square[i][j] = key;
}

int[][] matrix = new int[n][n];

for(int k = 0; k < matrix.length; k++) {
for(int l = 0; l < matrix[0].length; l++) {
matrix[k][l] = square[k+1][l+1];
}
}

return matrix;
}

public static void main(String[] args) {
int[][] magic = Matrix.magicOdd(5);
for(int k = 0; k < magic.length; k++) {
for(int l = 0; l < magic[0].length; l++) {
System.out.print(magic[k][l] + " ");
}
System.out.println();
}
}
}


짝수 마방진 


 

 

 C

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

#define N 6
#define SWAP(x,y) {int t; t = x; x = y; y = t;}

void magic_o(int [][N], int);
void exchange(int [][N], int);

int main(void) {
int square[N][N] = {0};
int i, j;

magic_o(square, N/2);
exchange(square, N);

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

return 0;
}

void magic_o(int square[][N], int n) {
int count, row, column;

row = 0;
column = n / 2;

for(count = 1; count <= n*n; count++) {
square[row][column] = count; // 填A
square[row+n][column+n] = count + n*n; // 填B
square[row][column+n] = count + 2*n*n; // 填C
square[row+n][column] = count + 3*n*n; // 填D
if(count % n == 0)
row++;
else {
row = (row == 0) ? n - 1 : row - 1 ;
column = (column == n-1) ? 0 : column + 1;
}
}
}

void exchange(int x[][N], int n) {
int i, j;
int m = n / 4;
int m1 = m - 1;

for(i = 0; i < n/2; i++) {
if(i != m) {
for(j = 0; j < m; j++) // 처리규칙 1
SWAP(x[i][j], x[n/2+i][j]);
for(j = 0; j < m1; j++) // 처리규칙 2
SWAP(x[i][n-1-j], x[n/2+i][n-1-j]);
}
else { // 처리규칙 3
for(j = 1; j <= m; j++)
SWAP(x[m][j], x[n/2+m][j]);
for(j = 0; j < m1; j++)
SWAP(x[m][n-1-j], x[n/2+m][n-1-j]);
}
}
}

Java

public class Matrix {
public static int[][] magic22mp1(int n) {
int[][] square = new int[n][n];

magic_o(square, n/2);
exchange(square, n);

return square;
}

private static void magic_o(int[][] square, int n) {
int row = 0;
int column = n / 2;

for(int count = 1; count <= n*n; count++) {
square[row][column] = count; // 填A
square[row+n][column+n] = count + n*n; // 填B
square[row][column+n] = count + 2*n*n; // 填C
square[row+n][column] = count + 3*n*n; // 填D
if(count % n == 0)
row++;
else {
row = (row == 0) ? n - 1 : row - 1 ;
column = (column == n-1) ? 0 : column + 1;
}
}
}

private static void exchange(int[][] x, int n) {
int i, j;
int m = n / 4;
int m1 = m - 1;

for(i = 0; i < n/2; i++) {
if(i != m) {
for(j = 0; j < m; j++) // 처리규칙 1
swap(x, i, j, n/2+i, j);
for(j = 0; j < m1; j++) // 처리규칙 2
swap(x, i, n-1-j, n/2+i, n-1-j);
}
else { // 처리규칙 3
for(j = 1; j <= m; j++)
swap(x, m, j, n/2+m, j);
for(j = 0; j < m1; j++)
swap(x, m, n-1-j, n/2+m, n-1-j);
}
}
}

private static void swap(int[][] number,
int i, int j, int k, int l) {
int t;
t = number[i][j];
number[i][j] = number[k][l];
number[k][l] = t;
}

public static void main(String[] args) {
int[][] magic = Matrix.magic22mp1(6);

for(int k = 0; k < magic.length; k++) {
for(int l = 0; l < magic[0].length; l++) {
System.out.print(magic[k][l] + " ");
}
System.out.println();
}
}
}



 4n 마방진

 
 
 
C

 

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

#define N 8

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

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;
}


Java

 

public class Matrix {
public static int[][] magicFourN(int n) {
int[][] square = new int[n+1][n+1];

for(int j = 1; j <= n; j++) {
for(int 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;
}
}

int[][] matrix = new int[n][n];

for(int k = 0; k < matrix.length; k++) {
for(int l = 0; l < matrix[0].length; l++) {
matrix[k][l] = square[k+1][l+1];
}
}

return matrix;
}

public static void main(String[] args) {
int[][] magic = Matrix.magicFourN(8);
for(int k = 0; k < magic.length; k++) {
for(int l = 0; l < magic[0].length; l++) {
System.out.print(magic[k][l] + " ");
}
System.out.println();
}
}
}

 

 








 Magic Squares
 
 
 
How to construct Magic Squares - by chandru arni
 
 
 
Richard Wiseman Magic Square
 

 
  
Freemasonry's Secret Foundation ( Part 1 )
 
 
 
Freemasonry's Secret Foundation (Part 2)
 
 

참고          참고         참고         참고         참고         참고         참고         참고         참고         참고

참고          참고         참고         참고         참고         참고         참고         참고         참고         참고


'유모어' 카테고리의 다른 글

Yo-Yo  (0) 2009.01.13
spray paint art  (0) 2009.01.11
Wingsuits  (0) 2009.01.09
camera mask  (0) 2009.01.06
Fire Balls  (0) 2009.01.05