| Java Basics Track for A290/A590 Spring 2008 Reference: Cay Horstmann's Core Java volume I (Fundamentals) For additional references available on-line for free from Books24x7 please see the Intro track |
Tue Apr 8 10:56:34 EDT 2008 Problem Three:
n-by-n squares;
- it works only if
n is odd.
- Place a
1 in the middle of the bottom row.
- After
k has been placed in the (i, j) square,
place k+1 into the square to the right and down,
wrapping around the borders.
- However,
- if the square to the right and down has already been filled, or
- if you are in the lower right corner,
then you must move to the square straight up instead.
Here's the 5-by-5 square that you get if you follow this method:
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
Check that the square above is magic.
Calculate the 3-by-3, 7-by-7 and 13-by-13 magic squares.
Here's how my code is working:
-bash-3.1$ java Magic 3
4 9 2
3 5 7
8 1 6
-bash-3.1$ java Magic 5
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
-bash-3.1$ java Magic 7
22 31 40 49 2 11 20
21 23 32 41 43 3 12
13 15 24 33 42 44 4
5 14 16 25 34 36 45
46 6 8 17 26 35 37
38 47 7 9 18 27 29
30 39 48 1 10 19 28
-bash-3.1$ java Magic 9
37 48 59 70 81 2 13 24 35
36 38 49 60 71 73 3 14 25
26 28 39 50 61 72 74 4 15
16 27 29 40 51 62 64 75 5
6 17 19 30 41 52 63 65 76
77 7 18 20 31 42 53 55 66
67 78 8 10 21 32 43 54 56
57 68 79 9 11 22 33 44 46
47 58 69 80 1 12 23 34 45
-bash-3.1$ java Magic 11
56 69 82 95 108 121 2 15 28 41 54
55 57 70 83 96 109 111 3 16 29 42
43 45 58 71 84 97 110 112 4 17 30
31 44 46 59 72 85 98 100 113 5 18
19 32 34 47 60 73 86 99 101 114 6
7 20 33 35 48 61 74 87 89 102 115
116 8 21 23 36 49 62 75 88 90 103
104 117 9 22 24 37 50 63 76 78 91
92 105 118 10 12 25 38 51 64 77 79
80 93 106 119 11 13 26 39 52 65 67
68 81 94 107 120 1 14 27 40 53 66
-bash-3.1$
Last updated by Adrian German for A290/A590 on Sun Apr 06 14:39:38 EST 2008