# Filling array with numbers from given range so that sum of adjacent numbers is square number

Problem: Fill all the cells using distinct numbers from <1,25> set, so that sum of two adjacent cells is a square number.

(source: http://grymat.im.pwr.wroc.pl/etap1/zad1etp1213.pdf; numbers 20 and 13 have been given)

I've already solved this problem analytically and now I would like to approach it using an algorithm.

I would like to know how should I approach these kind of problems in general (not a solution, just a point for me to start).

• i'm curious: why are the cells arranged with two bumps but not in a horizontal straight line? Nov 4, 2012 at 16:45
• When in doubt just bruteforce it. Try every combination and test that it satisfies your condition. You could speed things up by testing adjacent cells as you produce the combinations.
– Asaf
Nov 4, 2012 at 17:11
• @mauris - I'm guessing it's because the cells better fit the A4 page layout this way. Nov 4, 2012 at 17:13
• @Asaf, I know it can be done using BF but I'm looking for something more sophisticated. Nov 4, 2012 at 17:14
• 20 + 21 = 41, that's not square. He could use 5 or 16 there. Nov 5, 2012 at 12:00