0
$\begingroup$

I found a programming problem containing mathematics. I was unable to find an algorithm for this one. Does anyone have any idea how to construct a given matrix?

I would like to construct a $9\times 13$ matrix, where each element is a digit $0,\ldots, 9$; this matrix contains numbers that are formed by concatenating consecutive digits on a straight line starting from any position and going to any of $8$ possible directions horizontally vertically or diagonally; the resulting matrix must contain the squares of $1$ to $100$, i.e. $1^2 = 1$ to $100^2 = 10000$ can be found in the matrix.

I found that I need to find only $81$ numbers to the matrix:

def substringSieve(string_list):
    string_list.sort(key=lambda s: len(s), reverse=True)
    out = []
    for s in string_list:
        if not any([s in o for o in out] ) and not any([s[::-1] in o for o in out]):
            out.append(s)
    return out

A = list()
for i in range(1,101):
    A.append(str(i*i))
B = substringSieve(A)
print(B)
print(len(B))

outputs

['5625', '1764', '2704', '1681', '8281', '2116', '5184', '2809', '729', '841', '3249', '7056', '4900', '2401', '8649', '3969', '9604', '256', '361', '676', '7396', '5929', '3364', '529', '3481', '5041', '5329', '8464', '3844', '1024', '3025', '1600', '1521', '2304', '121', '6241', '6561', '1225', '1849', '8100', '289', '6400', '4096', '5776', '3600', '3721', '1369', '1444', '1156', '1936', '7744', '9025', '2601', '9216', '3136', '7225', '484', '784', '576', '9409', '6889', '4761', '2025', '4356', '4624', '4489', '7569', '196', '4225', '1296', '6084', '169', '10000', '5476', '8836', '7921', '2500', '1089', '6724', '2916', '2209']
81

I have seen that there is a solution for the case 11x11 matrix in https://stackoverflow.com/questions/3382859/solving-a-recreational-square-packing-problem?noredirect=1&lq=1 . But I'm not sure how can I generalize that algorithm for smaller rectangle.

I also found think that brute force search or best first search is too slow. I think there might be a solution using simulated annealing, genetic algorithm or bin packing algorithm. I also wondered if one can apply Markov chains somehow to find the grid. Unfortunately those seems to be too hard for me to implemented at current skills.

So is there any ideas how this can be solved or what kind of mathematics or programming one should learn to solve that problem? Will the article in https://comserv.cs.ut.ee/home/files/Thesis_tomozov.pdf?study=ATILoputoo&reference=C59DCE2177378FCBA13A04AE9EE6BFF32105541E help to solve the problem?

  • 2
    Note that all two-digit squares (of $x$), and any three-digit squares that are a multiple of $5$, will occur as part of a four-digit square of $100{-}x$2017-02-08
  • 0
    @Joffan I think the code above finds the minimal list of numbers that I have to put to the grid.2017-02-08
  • 0
    There seems to be a bug.2017-02-08
  • 0
    The reverse check only eliminates $169$, $441$ and $1089$. So $81$ strings to fit in.2017-02-08
  • 1
    A word-search algorithm, such as the following link, might provide a good starting point: http://stackoverflow.com/a/23436137/54777762017-02-09

0 Answers 0