Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/magic-squares-of-odd-order/oracle.py # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / magic-squares-of-odd-order / oracle.py` 23 lines ยท 1053 bytes Raw Blame History 1 # Independent reference for "Magic squares of odd order" (Python 3, standard library only). 2 # expected.txt is this program's stdout; omc never produced it. 3 # Cross-check, WolframLanguageEvaluator: Table[n (n^2 + 1)/2, {n, {3, 5, 9}}] -> {15, 65, 369} 4 # the cells are kept in a dict keyed by (row, column) on a torus, and the result is checked: every row, 5 # column and both diagonals must sum to the magic constant 6 def siamese(n): 7 m = {} 8 r, c = 0, n // 2 9 for k in range(1, n * n + 1): 10 m[r, c] = k 11 nxt = ((r - 1) % n, (c + 1) % n) 12 r, c = ((r + 1) % n, c) if nxt in m else nxt 13 return [[m[i, j] for j in range(n)] for i in range(n)] 14 for t, n in enumerate((3, 5, 9)): 15 if t: 16 print() 17 sq = siamese(n) 18 s = n * (n * n + 1) // 2 19 assert all(sum(row) == s for row in sq) and all(sum(col) == s for col in zip(*sq)) 20 assert sum(sq[i][i] for i in range(n)) == s == sum(sq[i][n - 1 - i] for i in range(n)) 21 for row in sq: 22 print("".join(f"{v:4}" for v in row)) 23 print("magic constant:", s)