Skip to content

Commit 7c94540

Browse files
author
Raghav Dua
authored
Merge pull request algorithm-visualizer#198 from archie94/magic-square
Implementation of Magic Square
2 parents 22d26de + 4d439bf commit 7c94540

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

algorithm/category.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@
106106
"etc": {
107107
"list": {
108108
"flood_fill": "Flood Fill",
109-
"cellular_automata": "Cellular Automata",
110-
"create_maze": "Create Maze"
109+
"cellular_automata": "Cellular Automata",
110+
"create_maze": "Create Maze",
111+
"magic_square": "Magic Square"
111112
},
112113
"name": "Uncategorized"
113114
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var i = Math.floor (n/2);
2+
var j = n-1;
3+
4+
for ( var num = 1; num <= n*n; ) {
5+
logTracer._print ( 'i = ' + i );
6+
logTracer._print ( 'j = ' + j );
7+
8+
if( i == -1 && j == n ) {
9+
j = n - 2;
10+
i = 0;
11+
12+
logTracer._print ( 'Changing : ' );
13+
logTracer._print ( 'i = ' + i );
14+
logTracer._print ( 'j = ' + j );
15+
} else {
16+
if ( j == n ) {
17+
j = 0;
18+
logTracer._print ( 'Changing : ' + 'j = ' + j);
19+
}
20+
if ( i < 0 ) {
21+
i = n-1;
22+
logTracer._print ( 'Changing : ' + 'i = ' + i );
23+
}
24+
}
25+
26+
if ( A[i][j] > 0 ) {
27+
logTracer._print ( ' Cell already filled : Changing ' + ' i = ' + i + ' j = ' + j );
28+
j -= 2;
29+
i++;
30+
continue;
31+
} else {
32+
A[i][j] = num++;
33+
tracer._notify( i, j, A[i][j] )._wait ();
34+
tracer._denotify ( i, j );
35+
tracer._select ( i, j )._wait ();
36+
}
37+
j++;
38+
i--;
39+
}
40+
41+
logTracer._print ( 'Magic Constant is ' + n*(n*n+1)/2 );
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var n = 7;
2+
var A = new Array (n);
3+
for (var i = n - 1; i >= 0; i--) {
4+
A[i] = new Array (n);
5+
}
6+
7+
for ( var i = n -1; i >= 0; i-- ) {
8+
for ( var j = n - 1; j >= 0; j-- ) {
9+
A[i][j] = 0;
10+
}
11+
}
12+
13+
var tracer = new Array2DTracer ('Magic Square')._setData(A);
14+
var logTracer = new LogTracer ( 'Console' );

algorithm/etc/magic_square/desc.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Magic Square": "In recreational mathematics, a magic square is an arrangement of distinct numbers (i.e., each number is used once), usually integers, in a square grid, where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number, called the magic constant. A magic square has the same number of rows as it has columns, and in conventional math notation, 'n' stands for the number of rows (and columns) it has. Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being of order n. A magic square that contains the integers from 1 to n2 is called a normal magic square. (The term magic square is also sometimes used to refer to any of various types of word squares.)",
3+
"Complexity": {
4+
"time": " O(N<sup>2</sup>)",
5+
"space": "O(N<sup>2</sup>)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Magic_square'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Magic Square"
12+
}
13+
}

0 commit comments

Comments
 (0)