From the same source, another interesting property of a magic square is that "if every number in a magic square is subtracted from
This time, though, we would like to present our own "magic square". This magic square does not comply with the requirements to strictly be called magic square, but as it was very entertaining to create, we decided that it could be another type of magic square.
Can you tell from the following code how our magic square looks like?
MagicSquares.java
1 public class MagicSquares
2 {
3 private static int nElementsPerLine;
4 private static int startConstant;
5 private static int endConstant;
6 private static int element;
7
8 public static void print(int n){
9 element = n;
10 nElementsPerLine = 2 * element - 1;
11 startConstant = 0;
12 endConstant = 0;
13
14 for(int line = 0; line < nElementsPerLine; line++)
15 {
16 if(line < element)
17 {
18 startConstant = line;
19 endConstant = nElementsPerLine - line;
20 }
21 else
22 {
23 startConstant = nElementsPerLine - line - 1;
24 endConstant = line + 1;
25 }
26
27 for(int index = 0; index < nElementsPerLine; index++)
28 {
29 if(index < startConstant)
30 {
31 System.out.printf("%3d", element);
32 element--;
33 }
34 else if(index < endConstant)
35 {
36 System.out.printf("%3d", element);
37 }
38 else
39 {
40 element++;
41 System.out.printf("%3d", element);
42 }
43 }
44 System.out.println();
45 }
46 }
47
48 public static void main(String[] args){
49 MagicSquares.print(3);
50 }
51 }
52
53
Hint:
Look for patterns of symmetry that are controlled by the variables startConstant and endConstant and the way they affect the output on each line.
Solution:
In this example, our magic square looks like this:
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
Variable elements is initialized with the value of 3 (line 49), then variable nElementsPerLine is initialized using the value of the variable elements: 2 * 3 - 1 = 5 (line 10). The first for cycle controls the number of lines and the nested for controls the appearance of the line, thus dictating the order in which the numbers are printed to build concentric squares given a value. To accomplish this, we have to look at how startConstant and endConstant change values throughout the main for cycle:
1. Whenever the number of the line we want to print is smaller than the element (line 16), the value of the variable startConstant will be equal to that line, the variable endConstant will be equal to the number of elements in that line minus the number of line, and from this, in the second cycle when the variable startConstant is zero (meaning that we are printing the first line)(line 29), the flow of the program jumps to the second condition. In ths condition (line 34), the element will be printed repeatedly the number of times that a line is allowed to grow (in this case 5).
2. When the variable startConstant is greater than zero and the variable endConstant begins to get smaller, the flow can enter the first condition on the second cycle. In this condition, the element will be printed once, decremented by one and continue like this until the condition becomes false (lines 29-32).
3. Once the first and second conditions on the nested cycle become false, the program will jump to the third condition (line 38), then it will increase by one the value of the element and print that value until it reaches the original value of the variable element.
4. From here, the process will start backwards and it will end when just the third condition of the second cycle is true.
5. In the last line of the magic square, only the third condition of the nested cycle will be true, therefore, the original element will be printed repeatedly again.
You can try with different values to test this code and tell us what you think in the comments.