Print all 3 by 3 sturdy squares

Python 3, 169 168 164 bytes

Took the program I used to investigate these sturdy squares/rectangles and golfed it wayyyy down. Golfed off 4 bytes thanks to otakucode.

from itertools import*
r=range(1,10)
for p in permutations(r,6):
 x,y=p[0],p[5];q=p[:5]+(x+p[3]-p[2],y,y+p[1]-x,p[2]+y-x)
 if set(q)==set(r):print('%s %s %s\n'*3%q)

Explanation

Given a partially-filled sturdy square like this,

a b c
d e ?
g ? ?

The remaining three entries are uniquely determined, and are a+d-c, a+b-g, and c+g-a. So I generate all permutations of 0..8 with six elements, calculate the rest, and then check to see whether the set of this is the same as the set of 0..8. If it is, I print out the grid.


For reference, here's the original (with comments and extraneous code removed):

from itertools import permutations as P

n = 3
m = 3
permutes = P(range(m*n), m+n)

counter = 0
for p in permutes:
    grid = [p[:n]]
    for i in range(m-1):
        grid.append([p[n+i]]+[-1]*(n-1))
    grid[1][1] = p[-1]

    s = p[0]+p[1]+p[n]+p[-1]

    has = list(p)

    fail = 0
    for y in range(1,m):
        for x in range(1,n):
            if x == y == 1: continue

            r = s-(grid[y-1][x-1] + grid[y-1][x] + grid[y][x-1])

            if r not in has and 0 <= r < m*n:
                grid[y][x] = r
                has.append(r)
            else:
                fail = 1
                break

        if fail: break

    if not fail:
        counter += 1

print(counter)

Pyth, 38 34 33 32 bytes

Vfq2l{sMX2.DR2.:T5b.pS9Vc3NjdH)k

5 bytes saved in formatting by Jakube

1 byte saved by switching to Peter Taylor's substrings of length five, remove the middles approach

Takes about a minute and a half to run on my machine.

How it works at the high level:

  • Generate all permutations (.pS9)

  • Form length 5 substrings (.:T5)

  • Remove the center element of each (.DR2)

  • Append a newline to the center element, marking it with a necessarily different sum (X2 ... b)

  • Filter for the squares where all such sums are equal (fq2l{)

  • Format and print (V ... Vc3NjdH)k)


CJam, 40 38 bytes

A,1>e!3f/{2{2few:::+z}*:|,1=},Ma*Sf*N*

Thanks to @PeterTaylor for golfing off 2 bytes!

This finishes instantly using the Java interpreter. It works using the online interpreter as well, but it requires a little patience. Try it online.

Test run

$ cjam sturdy-squares.cjam | head -n 8
1 5 3
9 8 7
4 2 6

1 5 6
8 7 3
4 2 9

$ cjam sturdy-squares.cjam | tail -n 8

9 5 4
2 3 7
6 8 1

9 5 7
1 2 3
6 8 4
$

How it works

A,1>     e# Push [1 ... 9].
e!       e# Push the array of all permutations of that array.
3f/      e# Split each into rows of length 3.
{        e# Filter; push the permutation, then:
  2{     e#   Do the following twice:
    2few e#     Split each row into overlapping splices of length 2.
         e#       [a b c] -> [[a b] [b c]]
    :::+ e#     Reduce each innermost vector to its sum.
         e#       [[a b] [b c]] -> [a+b b+c]
    z    e#     Transpose rows with columns.
  }*     e#   The result is [[s t] [u v]], the sums of all 2x2 squares.
  :|     e#   Perform set union of the pairs of sums.
  ,1=    e#   Check if the length of the result is 1 (unique sum).
},       e# Keep the array if the result was 1.
{        e# For each kept array:
  Sf*    e#   Join the elements of its rows, separating by spaces.
  ~M     e#   Dump the resulting strings and an empty string on the stack.
}%       e# Collect everything in an array.
N*       e# Join the strings, separating by linefeeds.