#sdpsgssolutions #Magic Squares #AIO 2016 (Intermediate) # Open the input and output files. inputFile = open("magicin.txt", "r") outputFile = open("magicout.txt", "w") # Read how many numbers there will be myInput = inputFile.readline().split() A = int(myInput[0]) B = int(myInput[1]) C = int(myInput[2]) # A B D # C F G # E H I # Set D as the top-right cell E = 0 D = 0 F = 0 G = 0 H = 0 I = 0 #Find Total Total = A + B + D while Total != E + H + I: while E<=0: D = D + 1 Total = A + B + D # Use this to find E, the bottom-left cell E = Total - A - C # Is E positive? If not add 1 to D and try again # Increase F F = F + 1 # work out H, G, I: H = Total - B - F G = Total - C - F I = Total - D - G # Does it work? If not start again outputFile.write(str(A)+' '+str(B)+' '+str(D)+'\n'+str(C)+' '+str(F)+' '+str(G)+'\n'+str(E)+' '+str(H)+' '+str(I)) # Finally, close the input/output files. inputFile.close() outputFile.close()