Water Retention Homework 2010

Problem #1.  Find the water capacity for the following square and write your solution in code that will find the water capacity for any order 5 square

 

8

11

22

20

4

 

8

11

22

20

4

14

24

1

5

21

 

14

24

1

5

21

15

6

23

2

19

 

15

6

23

2

19

18

7

3

25

12

 

18

7

3

25

12

10

17

16

13

9

 

10

17

16

13

9

(1)

 

Method  1  :  list all pathways of escape from each interior cell to the exterior

                        There are only 3 unique cell positions in the interior  b2, b3, c3

   Step 1.  List all pathways for cell position c3

          

a1

a2

a3

a4

a5

b1

b2

b3

b4

b5

c1

c2

c3

c4

c5

d1

d2

d3

d4

d5

e1

e2

e3

e4

e5

 

                      

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(1)

                     Starting from  cell c3  and moving through cell b3 there are 5 paths

 

 

 

 

 

Step 2.  List all pathways for cell position b2

a1

a2

a3

a4

a5

b1

b2

b3

b4

b5

c1

c2

c3

c4

c5

d1

d2

d3

d4

d5

e1

e2

e3

e4

e5

 

Starting from cell b2 and moving to a2 or through b3 there are 18 paths

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(1)

      The number of pathways defined for b2 … obviously apply to b4,d2 and d4

 

 

 

 

 

 

Step 3.  List all pathways off the square for cell b3

 

a1

a2

a3

a4

a5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b1

b2

b3

b4

b5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

c1

c2

c3

c4

c5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

d1

d2

d3

d4

d5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

e1

e2

e3

e4

e5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(1)

 

Taking into account all the symmetrical redundancies :

For cell c3 = 20 paths  off the square x 1 =  20 paths

For cell b2 =36 paths off the square x 4 = 144 paths

For cell b3 =30 paths off the square x 4 = 120 paths

_______________________________________________

 Total paths order 5 square  =                      284 paths

 

 

Once the paths have been defined sorting all the paths for the path with the least obstruction off the square is a straight forward process.

Example  #1  of code that will find the capacity for any order 5 square:

  Local Int a, b, c, d, e, f, g, h, z, r, s

  ' a e b

  ' f z g

  ' c h d

 

  a = MaxI(j22, MinI(j21, j12))

  b = MaxI(j42, MinI(j41, j52))

  c = MaxI(j24, MinI(j14, j25))

  d = MaxI(j44, MinI(j54, j45))

 

  e = MaxI(j32, MinI(j31, a, b))

  f = MaxI(j23, MinI(j13, a, c))

  g = MaxI(j43, MinI(j53, b, d))

  h = MaxI(j34, MinI(j35, c, d))

 

  z = MaxI(j33, MinI(e, f, g, h))

 

  If (e > j32) And (e > z) Then e = MaxI(j32, z)

  If (f > j23) And (f > z) Then f = MaxI(j23, z)

  If (g > j43) And (g > z) Then g = MaxI(j43, z)

  If (h > j34) And (h > z) Then h = MaxI(j34, z)

 

  If a > j22 Then r = MinI(e, f) : If a > r Then a = MaxI(j22, r)

  If b > j42 Then r = MinI(e, g) : If b > r Then b = MaxI(j42, r)

  If c > j24 Then r = MinI(f, h) : If c > r Then c = MaxI(j24, r)

  If d > j44 Then r = MinI(g, h) : If d > r Then d = MaxI(j44, r)

 

  If z = j33

 

    If e > j32 Then r = MinI(a, b) : If e > r Then e = MaxI(j32, r)

    If f > j23 Then r = MinI(a, c) : If f > r Then f = MaxI(j23, r)

    If g > j43 Then r = MinI(b, d) : If g > r Then g = MaxI(j43, r)

    If h > j34 Then r = MinI(c, d) : If h > r Then h = MaxI(j34, r)

 

    If a > j22 Then r = MinI(e, f) : If a > r Then a = MaxI(j22, r)

    If b > j42 Then r = MinI(e, g) : If b > r Then b = MaxI(j42, r)

    If c > j24 Then r = MinI(f, h) : If c > r Then c = MaxI(j24, r)

    If d > j44 Then r = MinI(g, h) : If d > r Then d = MaxI(j44, r)

 

    If e > j32 Then r = MinI(a, b) : If e > r Then e = MaxI(j32, r)

    If f > j23 Then r = MinI(a, c) : If f > r Then f = MaxI(j23, r)

    If g > j43 Then r = MinI(b, d) : If g > r Then g = MaxI(j43, r)

    If h > j34 Then r = MinI(c, d) : If h > r Then h = MaxI(j34, r)

 

    If a > j22 Then r = MinI(e, f) : If a > r Then a = MaxI(j22, r)

    If b > j42 Then r = MinI(e, g) : If b > r Then b = MaxI(j42, r)

    If c > j24 Then r = MinI(f, h) : If c > r Then c = MaxI(j24, r)

    If d > j44 Then r = MinI(g, h) : If d > r Then d = MaxI(j44, r)

 

    If e > j32 Then r = MinI(a, b) : If e > r Then e = MaxI(j32, r)

    If f > j23 Then r = MinI(a, c) : If f > r Then f = MaxI(j23, r)

    If g > j43 Then r = MinI(b, d) : If g > r Then g = MaxI(j43, r)

    If h > j34 Then r = MinI(c, d) : If h > r Then h = MaxI(j34, r)

 

  End If

 

  r = 0 : s = 0

  If j22 < a Then r += a - j22 : s = Bset(s, 0)

  If j42 < b Then r += b - j42 : s = Bset(s, 2)

  If j24 < c Then r += c - j24 : s = Bset(s, 6)

  If j44 < d Then r += d - j44 : s = Bset(s, 8)

  If j32 < e Then r += e - j32 : s = Bset(s, 1)

  If j23 < f Then r += f - j23 : s = Bset(s, 3)

  If j43 < g Then r += g - j43 : s = Bset(s, 5)

  If j34 < h Then r += h - j34 : s = Bset(s, 7)

  If j33 < z Then r += z - j33 : s = Bset(s, 4)

 

  cnt(sym(s), r)++

 

 

(1)

 

 

 

 

 

 

 

Example  #2  of code that will find the capacity for any order 5 square:

 

 

 

Local Int x, y, r = 0, s = 0, b = 0

  ArrayFill v(), 0

  For y = 2 To 4

    For x = 2 To 4

      Barrier = 25

      v(x, y)++

      Path(x, y, 0)

      v(x, y)--

      If Barrier > j(x, y)

        r += Barrier - j(x, y)

        s = Bset(s, b)

      End If

      b++

    End For

  End For

  cnt(sym(s), r)++

 

Proc Path(ByVal x As Int, ByVal y As Int, ByVal h As Int)

  If v(x, y) > 1 Then Exit Proc

  h = MaxI(h, j(x, y))

  If (x = 1) Or (x = 5) Or (y = 1) Or (y = 5)

    Barrier = MinI(Barrier, h)

  Else

    v(x - 1, y)++

    v(x + 1, y)++

    v(x, y - 1)++

    v(x, y + 1)++

    Path(x - 1, y, h)

    Path(x + 1, y, h)

    Path(x, y - 1, h)

    Path(x, y + 1, h)

    v(x - 1, y)--

    v(x + 1, y)--

    v(x, y - 1)--

    v(x, y + 1)--

  End If

 

(1)

 

 

Problem #2 :  label all unique water retention patterns for the order 5 square

Water Patterns(1)

Problem #3.  Write code that will find the water capacity of any order of square

 

 

The number of paths off the square and the number of water patterns increase exponentially as the order of the square increases.   The table below shows that there are 4211744 different water patterns for a order 7 square.  The list all pathways method is losing its appeal as a method to solve this problem

 

 

 

Order n

Number of water patterns

 

3

2

 

4

6

 

5

102

 

6

8548

 

7

4211744

 

8

8590557312

 

9

7.03689E+13

 

 

 

 

(1)

 

 

The table above shows the necessity for abandoning the list all pathway process to determine the water capacity for  squares   >  order 5.   A general algorithm is needed that will determine the water capacity of any order of square.

 

 

 

 

 

 

 

 

Water Retention Algorithm  (Python) (2)

 

def process(square):

  n = len(square) # square is a list of lists

  bounds = [n*[n*n] for i in range(n)]

  queue = [] # lousy implementation, just for algorithm testing

  # head of queue is last element

  # big numbers for high priority

  for i in range(n-1):

    bounds[i][0] = square[i][0]

    queue.append((-bounds[i][0],i,0))

    bounds[n-1][i] = square[n-1][i]

    queue.append((-bounds[n-1][i],n-1,i))

    bounds[i+1][n-1] = square[i+1][n-1]

    queue.append((-bounds[i+1][n-1],i+1,n-1))

    bounds[0][i+1] = square[0][i+1]

    queue.append((-bounds[0][i+1],0,i+1))

  while queue:

    queue.sort()

    (b,i,j) = queue.pop()

    for (di,dj) in [(-1,0),(1,0),(0,-1),(0,1)]:

      u=i+di; v=j+dj

      if 0<=u<n and 0<=v<n:

        b1 = max(square[u][v],-b)

        if b1 < bounds[u][v]:

          bounds[u][v] = b1

          queue.append((-b1,u,v))

  return bounds

 

def capacity(square):

  bounds = process(square)

  n = len(square)

  s = 0

  for i in range(n):

    for j in range(n):

      delta = bounds[i][j]-square[i][j]

      print bounds[i][j],delta

      if delta>0: s += delta

  return s

 

result = capacity([[57,38,31,47,32,40,45,46,33],

[27,58,60,23,66,9,15,72,39],

[29,61,12,65,8,69,77,11,37],

[30,64,6,81,3,80,7,62,36],

[48,19,78,14,41,68,4,63,34],

[44,20,75,2,79,1,76,18,54],

[42,71,5,13,74,17,70,21,56],

[43,10,67,73,16,59,22,24,55],                  

[49,28,35,51,50,26,53,52,25]])

 

print(result)

 

(2)

 

 

 

Explanation of how the water retention algorithm works (1)

 

The water number table in blue to the right is generated by setting all interior cells to the maximum number for the square and leaving all the exterior values from the original square.

Original Square                                                          Water Number   W - number

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

16

16

8

9

6

7

12

 

9

16

16

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

No cell can retain more water than the w-number.

 

 

 

 

Therefore we just have to consider how much water flows away from each inner cell.

 

 

 

 

 

 

 

 

 

Search the lowest w-number.

 

 

 

 

 

 

w = 1

 

 

 

 

 

 

 

 

This cell already has got its final water level.

 

 

 

 

Let the water flow from the neighbor cells, if possible.

 

 

 

From 14 and 12 no water can flow to 1.

 

 

 

 

 

Because 14 and 12 are the original numbers.

 

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

16

16

8

9

6

7

12

 

9

16

16

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Search the lowest w-number.

 

 

 

 

 

 

w = 2

 

 

 

 

 

 

 

 

This cell already has got its final water level.

 

 

 

 

Let the water flow from the neighbour cells, if possible.

 

 

 

From 3 and 13 no water can flow to 2.

 

 

 

 

 

But from the cell below (16) 5 units of water flow to 2.

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

16

11

8

9

6

7

12

 

9

16

16

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Search the lowest w-number.

 

 

 

 

 

 

w = 3

 

 

 

 

 

 

 

 

This cell already has got its final water level.

 

 

 

 

Let the water flow from the neighbor cells, if possible.

 

 

 

From the cell below (16) 6 units of water flow to 3.

 

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

10

11

8

9

6

7

12

 

9

16

16

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Repeat this steps for  w = 4, 5, 8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

10

11

8

9

6

7

12

 

9

16

16

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Now it's the turn for the cell with w-number 9

 

 

 

 

From the right neighbor cell water flows to 9.

 

 

 

 

But in this case not all the water.

 

 

 

 

 

Because the original number of the right neighbor cell is 6.

 

 

 

And therefore 9 is a barrier.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

10

11

8

9

6

7

12

 

9

9

16

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Go on with the new 9, because this is the lowest w-number.

 

 

 

Again water flows from the right neighbour cell.

 

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

10

11

8

9

6

7

12

 

9

9

9

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Go on until all cells have got their final water level.

 

 

 

 

 

 

 

 

 

 

 

 

 

16

3

2

13

 

16

3

2

13

5

10

11

8

 

5

10

11

8

9

6

7

12

 

9

9

9

12

4

15

14

1

 

4

15

14

1

 

 

 

 

 

 

 

 

 

Subtract original numbers from w-numbers and add the resulting numbers.

 

 

0

0

0

0

0

0

0

0

0

3

2

0

0

0

0

0

(1)

The end result shows that the original square retains 3 + 2  = 5 units of water

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Problem # 4.  Write code that will predict the pattern for maximum water retention for any normal number square  < order 250

 

 

34

 

35

36

 

 

33

20

49

1

2

37

 

 

48

3

4

5

6

38

47

7

8

9

10

11

39

46

12

13

14

15

16

40

 

45

17

18

19

41

 

 

 

44

43

42

 

 

 

 

35

 

36

37

 

 

34

18

49

1

2

38

 

 

48

3

4

5

6

39

47

7

8

9

10

11

40

46

12

13

14

15

41

 

 

45

16

17

42

19

33

 

 

44

43

 

32

 

(1)

The  number squares above show the two patterns that provide maximum retention for the order 7 square with 488 units water retained.

      Top square        Lake = 475 units    ( 19 *35) – S (19)      :   Pond = 13 units

     Lower square    Lake = 459 units    (17*36) – S(17)          :  Pond = 15 + 14

Lake = width N – 2, width N-2  :  N = order of square  (1)

Pond = all other water retaining patterns

 

 

 

Normal number squares are defined as a square where all the numbers 1 to N*N fill the cells where the order of the square = N.  A order 7 square (N = 7) would contain the numbers 1 to 49.

 

The pattern for maximum water retention for normal number squares can be produced for any order of normal number square.  This pattern may help predict the pattern of maximum retention for magic squares.

 

Retention patterns for order 3,4,8,11 show perfect symmetry.

Retention patterns for order 5,7,30,58 have two patterns possible for maximum retention  ( From Walter Trump’s program )

 

 

 

 

 

 

 

 

 

 

 

 

                            

 

 

 

 

Problem #5.  Give examples for maximum retention for order 7 Magic Square

 

 

Magic Square of order 7 that retains 365 units of water

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5

33

44

39

34

18

2

 

175

 

 

 

 

 

 

20

49

8

15

11

48

24

 

175

 

 

 

 

 

 

37

14

23

29

30

6

36

 

175

 

 

 

 

 

 

43

12

22

17

26

10

45

 

175

 

 

 

 

 

 

38

1

27

21

32

16

40

 

175

 

 

 

 

 

 

28

47

9

13

7

46

25

 

175

 

 

 

 

 

 

4

19

42

41

35

31

3

 

175

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

175

 

175

175

175

175

175

175

175

 

175

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

34*21 =

 

714

 

 

 

 

 

 

 

 

blue sum =

349

 

 

 

 

 

 

 

Retained Water

365

 

 

 

 

 

(1)

 

 

 

 

 

 

 

 

 

Magic Square of order 7 that retains 378 units of water

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8

33

25

38

42

22

7

 

175

 

 

 

 

 

 

34

19

46

5

2

49

20

 

175

 

 

 

 

 

 

30

45

28

12

11

10

39

 

175

 

 

 

 

 

 

41

4

14

31

27

15

43

 

175

 

 

 

 

 

 

40

1

24

29

32

13

36

 

175

 

 

 

 

 

 

16

47

3

23

17

48

21

 

175

 

 

 

 

 

 

6

26

35

37

44

18

9

 

175

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

175

 

175

175

175

175

175

175

175

 

175

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

35*19 + 33 =

698

 

 

 

 

 

 

 

 

blue sum =

320

 

 

 

 

 

 

 

Retained Water

378

 

 

 

 

 

 

(1)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Semi-magic Square of order 7 that retains 394 units of water

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3

33

32

41

43

18

5

 

175

 

 

 

 

 

 

 

 

34

1

45

9

14

48

24

 

175

 

 

 

 

 

 

 

 

31

46

16

20

21

6

35

 

175

 

 

 

 

 

 

 

 

44

11

17

28

25

13

37

 

175

 

 

 

 

 

 

 

 

40

10

22

23

26

12

42

 

175

 

 

 

 

 

 

 

 

19

47

7

15

8

49

30

 

175

 

 

 

 

 

 

 

 

4

27

36

39

38

29

2

 

175

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

175

 

175

175

175

175

175

175

175

 

125

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

35*19+33=

698

 

 

 

 

 

 

 

 

 

 

blue sum =

304

 

 

 

 

 

 

 

 

 

Retained Water

394

 

 

 

 

 

 

 

(1)

 

 

 

 

 

 

 

Problem #6.  Give latest maximum retention for order 9 MS

 

I find it difficult to manipulate the order 9 magic square to find is maximum water retention.   My best effort to date starts with Walter Trump’s order 7 pandiagonal associated MS with 284 units retained.  I take the values of the 7x7 square > 25 and recompliment them (82 – n).  That provides the interior 49 values ..which the F1 compiler can complete for the order 9 MS.  The maximum from this endeavour = 1014 units retained.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Problem #7.  give  examples of a order 25 magic square that demonstrates the following

  a) lake   b) largest percentage of cells retaining water  c) maximum number of ponds

a.     lake

 

(1)

 

 

 

b Highest percentage of cells retaining water

(3)

 

 

 

 

 

 

 

 

c  maximum number of ponds

(4)

 

 

 

 

 

 

 

 

 

Problem #8.  Using a random number generator create a population of 1000 squares and find the average water retention for that population for squares from order 3 to 250 and number ranges from 1 to 2 , to 1 to 6.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2

1

2

1

2

2

1

1

1

2

2

2

1

1

2

2

2

1

2

2

1

1

2

1

2

 

 

 

 

2

2

1

2

2

2

1

1

1

2

1

2

2

1

1

1

2

1

2

1

2

2

1

2

2

 

 

 

 

1

1

1

1

2

1

2

1

1

2

1

2

2

1

1

2

1

1

1

2

2

2

1

2

2

 

 

 

 

2

2

2

1

2

1

2

2

1

2

1

2

2

2

2

1

1

1

2

1

1

1

1

1

1

 

 

 

 

1

1

1

1

1

1

1

2

1

1

1

1

1

2

2

2

2

1

2

1

1

2

2

1

2

 

 

 

 

2

2

1

1

1

1

2

2

2

2

1

2

1

2

2

1

2

2

2

1

1

1

1

2

2

 

 

 

 

2

2

2

1

2

2

2

2

1

2

2

2

2

2

2

1

2

1

1

2

2

1

2

1

2

 

 

 

 

1

1

1

1

2

2

1

2

1

2

2

1

1

1

1

2

2

2

2

2

2

1

2

2

1

 

 

 

 

1

1

1

2

1

2

2

2

1

1

2

1

2

1

1

1

1

1

2

2

2

1

1

2

2

 

 

 

 

2

2

2

2

1

2

1

2

2

2

1

1

2

1

1

1

1

1

1

2

2

1

1

1

1

 

 

 

 

1

2

2

2

1

1

2

2

2

2

2

2

2

1

1

1

2

2

1

2

1

1

2

2

2

 

 

 

 

1

1

1

2

2

2

2

2

1

2

2

1

1

2

2

1

1

2

2

2

2

1

2

1

2

 

 

 

 

2

1

2

2

1

1

1

1

2

2

1

1

1

2

1

1

1

1

2

1

2

2

2

2

1

 

 

 

 

1

1

2

2

1

2

2

1

1

1

2

2

2

2

1

2

2

2

2

1

2

1

2

2

1

 

 

 

 

2

1

1

2

1

1

1

1

1

2

1

2

2

2

2

1

1

2

1

2

2

2

2

2

1

 

 

 

 

1

2

2

1

2

2

2

1

1

1

1

1

2

1

1

2

2

2

2

2

2

1

1

2

1

 

 

 

 

1

1

1

2

2

1

2

1

2

1

2

2

1

2

2

1

1

2

1

1

1

2

1

1

2

 

 

 

 

2

1

2

2

2

2

1

2

2

1

1

2

2

1

1

1

1

2

2

2

2

2

2

1

1

 

 

 

 

2

1

1

1

1

2

2

2

1

1

1

2

2

2

2

2

2

2

2

1

1

1

2

2

1

 

 

 

 

2

1

1

2

2

1

1

2

1

2

2

1

2

2

2

2

1

1

1

2

1

2

1

1

1

 

 

 

 

2

1

1

1

1

2

1

1

2

1

2

2

2

2

1

1

1

1

1

1

2

2

2

1

1

 

 

 

 

1

2

2

1

1

2

1

1

2

1

1

2

1

1

1

1

1

2

1

1

1

2

1

1

1

 

 

 

 

1

2

1

2

2

1

2

2

1

2

1

1

1

1

2

1

1

1

2

1

1

1

1

1

2

 

 

 

 

1

2

2

1

1

2

1

1

2

2

2

2

1

1

1

1

1

1

2

1

2

2

2

2

1

 

 

 

 

2

1

2

2

2

2

2

2

2

1

2

1

2

2

2

2

1

2

1

1

1

2

2

1

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Water Retention Model  Number Range 1 to 2    

 

 

 

 

 

 

 

 

 

 

Order 25 Square

 random number placement

 

 

 

 

 

 

 

 

 

 

 

118 units retained this example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Barrier cells

 

 

 

 

 

 

 

 

 

show path off square for this cell

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cells containing water

 

 

 

 

 

 

path off the square

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  Number Range for Square

 

 

 

 

 

 

 

 

 

 

 

 

 

1 to 2

1 to 3

1 to 4

1 to 5

1 to 6

 

 

 

 

 

 

 

 

 

 

 

O

3

0.031

0.074

0.114

0.15

0.186

 

 

R

4

0.169

0.41

0.61

0.8

0.99

 

 

D

5

0.47

1.15

1.68

2.19

2.69

 

 

E

6

1

2.4

3.45

4.45

5.46

 

 

R

7

1.82

4.23

6

7.72

9.44

 

 

 

8

3

6.72

9.37

12

14.7

 

 

O

9

4.5

9.9

13.6

17.6

21.4

 

 

F

10

6.5

13.7

18.8

24.2

29.4

 

 

 

20

59

91

132

165

201

 

 

S

30

192

240

376

444

559

 

 

Q

40

419

460

767

865

1114

 

 

U

50

744

750

1306

1432

1873

 

 

A

60

1172

1114

1996

2141

2840

 

 

R

70

1697

1547

2838

3000

4005

 

 

E

80

2322

2053

3827

4000

5380

 

 

 

90

3053

2628

4974

5144

6960

 

 

 

100

3878

3275

6264

6442

8740

 

 

 

150

9503

7478

15000

15100

20720

 

 

 

200

17628

13660

27540

27500

37800

 

 

 

250

28200

21500

43700

43700

60000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Average Water Capacity for Population of Squares

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(5)

 

 The italic font in the lower left shows that the number range of 1 to 2 actually retains more water on average than the number range 1 to 3 when the order of the square > 50.

 

The development of the tools to determine the water capacity for magic squares has created some spinoffs.  The most interesting for myself is looking at the average capacity for a sample of squares with the parameters specified above. 

 

 

 

 

 

 

 

 

 

 

 

Reference:

(1)  Walter Trump

(2)  Gareth McCaughan

(3) Harry White

(4) Robert Dickter

(5) Craig Knecht

 

 

Acknowledgements:

        Walter Trump was kind enough to correct my previous mistakes and provide the essential guidance for the illustrations and contents above.   I looked at this problem with binoculars.  Trump looks with the Hubble Telescope.

        Gareth McCaughan  provided a real breakthrough in progress on the water retention problem when he contributed the idea and code for the water retention algorithm written in python.

        Harry White has both source code in  C ++ and a .exe version of the retention algorithm that will determine the water capacity of any order of square.  It is available in the download section of his excellent website on bordered magic squares.

       Colm Mulcahy suggested I run this idea by Ed Pegg and Martin Gardner.  His encouragement has been instrumental in giving this idea a wider exposure.

Notes:

 

 

 

Gareth McCaughan’s template algorithm  was purposely produced as a instructional item without any effort given to gain  efficiency.  Calculating the water capacity of a order 1000 square with the algorithm in this form takes about 10 minutes of computing time.  

Walter Trump over the years has designed efficient algorithms to produce  magic squares .   Trump wrote the code for a version of the retention algorithm that can calculate the water capacity of a magic square in a about the same time frame that it takes to produce the magic square.  This made it possible for him to completely sort the 570 billion order 5  semi magic squares , the 20 million order 7 pandiagonal associated magic squares as well as other large sets.

Trump in 2009 implemented the capability of using a random number generator to populate the variables in a algorithm that produces magic squares.  For MS orders > 12 and < 250  it is possible to specify the border for a lake with the largest available numbers.  The random number assignment process completes the square.  This process of chance can “search” for the largest example for water retention by mating it with the water retention algorithm.  This program is available upon request.