# arrange the numbers from 1 to 2n in pairs # so that the sum of each pair is a square # for what numbers is this possible? def GetPossibles(max): for n in range(1,max): # make a list of the needed squares def getSquares(n): squares = [] i=2 while i**2<4*n: squares.insert(0,i**2) i = i+1 # print squares return squares # this makes a list of lists: # what you can add to each number to get a square # the first element, being element 0, will be ignored # after that, each list corresponds to its index def getOptions(n): theOptions = [None] squares = getSquares(n) for number in range (1,2*n+1): complements=[] for square in squares: if (square > number) and (square != 2 * number) and (square - number < 2*n+1): complements.append(square - number) theOptions.append(complements) # print theOptions return theOptions # pairs is the list we are building for n # available is a list of the unused numbers def getPairs(n,pairs,available): if len(pairs) == n: return pairs number = available[0] for partner in theOptions[number]: if partner in available: newPair = (number,partner) result = getPairs(n,pairs + [newPair],[x for x in available if x not in newPair]) if result is not None: return result return None theOptions = getOptions(n) theOutput = getPairs(n,[],range(2*n,0,-1)) if theOutput != None: print n GetPossibles(44)