Python 3.5, 135 bajtów

5

CJam ( 39 37 bajtów)

A,1>e!{5ew{2Mtz}2*::+)-!},3f/Ma*Sf*N*

Demo online (ostrzeżenie: uruchomienie może potrwać minutę, powodując wyświetlenie w przeglądarce komunikatów „Przerwać ten skrypt?”).

Działa poprzez filtrowanie wszystkich możliwych siatek za pomocą 5ewmapowania

[a b c d e f g h i]

do

[[a b c d e]
 [b c d e f]
 [c d e f g]
 [d e f g h]
 [e f g h i]]

a następnie odrzucając środkowy element i środkowy element każdego innego elementu, aby uzyskać

[[a b d e]
 [b c e f]
 [d e g h]
 [e f h i]]

które są czterema kwadratami.

Peter Taylor
źródło
Wow, to wspaniale.
El'endia Starman

Odpowiedzi:

5

Python 3.5, 135 bajtów

from itertools import*
for x in permutations(range(1,10)):eval((("=="+"+x[%s]"*3)*4)[2:]%(*"013125367578",))and print("%d %d %d\n"*3%x)

Bezpośrednio sprawdza sumę każdego kwadratu minus środek. Najprawdopodobniej nadal można grać w golfa według „ itertoolsniepotrzebnej” reguły.

Sp3000
źródło
2

Python2 327 271 270 263 260 bajtów

z,v,s={},3,range(1,10)
while len(z)<376:
 for i in range(8):v=hash(`v`);s[i],s[v%9]=s[v%9],s[i]
 m=map(lambda i:sum(s[i:i+5])-s[i+2],[0,1,3,4]);T=tuple(s)
 if all(x==m[0] for x in m) and not T in z:
  z[T]=1;print '%i %i %i\n'*3 % tuple(s[0:3]+s[3:6]+s[6:9])

------------

To ... nie jest tak krótkie, ale nie korzysta z bibliotek. To losowo permutuje kwadrat, sprawdza magię, drukuje i zapisuje, aby zapobiec duplikowaniu. Po wydrukowaniu 376 unikalnych magicznych kwadratów zatrzymuje się.

Pożyczkę Pseudo Random Number Generator pożyczyłem od wpisu Keitha Randalla dla golfa o nazwie „ Zbuduj generator liczb losowych, który przejdzie testy Dieharda

z,v={},3
def R(x,y):global v;v=hash(`v`);return v
while len(z)<376:
 s=sorted(range(1,10),cmp=R)
 m=[sum(q) for q in map(lambda p:s[p[0]:p[1]+1]+s[p[2]:p[3]+1], [[i,i+1,i+3,i+4] for i in [0,1,3,4]] )]
 if all(x==m[0] for x in m) and not tuple(s) in z.keys():
  z[tuple(s)]=1;print '%i %i %i\n'*3 % tuple(s[0:3]+s[3:6]+s[6:9])

Grał w golfa

# each magic square is an array of 9 numbers
#
#for example [1 9 3 7 2 5 6 4 8] 
#
#represents the following square
#
#1 9 3
#7 2 5
#6 4 8
#
# to generate a random square with each number represented only once,
# start with [1 2 3 4 5 6 7 8 9] and sort, but use a random comparison
# function so the sorting process becomes instead a random permutation.
# 
# to check each 2x2 subsquare for sums, look at the indexes into the
# array: [[0,1,3,4] = upper left,[1,2,4,5] = upper right, etc.
#
# to keep track of already-printed magic squares, use a dictionary    
# (associative array) where the 9-element array data is the key. 

from random import *
def magic(s):
 quads=[]
 for a,b,c,d in [[0,1,3,4],[1,2,4,5],[3,4,6,7],[4,5,7,8]]:
  quads+=[s[a:b+1]+s[c:d+1]]
 summ=[sum(q) for q in quads]
 same= all(x==summ[0] for x in summ)
 #print quads
 #print 'sum',summ
 #print 'same',same
 return same

magicsquares={}
while len(magicsquares.keys())<376:
        sq = sorted(range(1,10),key=lambda x:random())
        if magic(sq) and not magicsquares.has_key(tuple(sq)):
                magicsquares[tuple(sq)]=1
                print sq[0:3],'\n',sq[3:6],'\n',sq[6:9],'\n'
Don Bright
źródło
Nic losowego nie musi się dziać. Istnieje dokładnie 376 różnych kwadratowych rozwiązań i musisz wydrukować każde z nich dokładnie raz.
Calvin's Hobbies
wydrukowałem dokładnie 376 różnych kwadratowych rozwiązań i wydrukowałem każde z nich dokładnie raz. losowość nie jest zbanowana w opisie, ani nie jest zbanowana w „standardowych lukach
don bright
W porządku, w porządku.
Calvin's Hobbies
Możesz użyć gorszego generatora liczb losowych, o ile daje on wszystkie potrzebne kwadraty.
lirtosiast
1

Ruby 133

a=[]
[*1..9].permutation{|x|[0,1,3,4].map{|i|x[i]+x[i+1]+x[i+3]+x[i+4]}.uniq.size<2&&a<<x.each_slice(3).map{|s|s*' '}*'
'}
$><<a*'

'

Proste podejście z użyciem siły brutalnej. Sprawdź to tutaj .

Cristian Lupascu
źródło
0

J, 83 bajty

([:;@,(<LF),.~[:(<@(LF,~":)"1@#~([:*/2=/\[:,2 2+/@,;._3])"2)(3 3)($"1)1+!A.&i.])@9:

Ta funkcja generuje ciąg znaków zawierający 376 mocnych kwadratów. Wykorzystuje brutalną siłę, generuje wszystkie permutacje od 1 do 9, kształtuje każdą z nich w tablicę 3x3 i filtruje ją, sprawdzając, czy sumy każdej podtablicy 2x2 są równe. Wykonuje się w pół sekundy.

Stosowanie

   f =: ([:;@,(<LF),.~[:(<@(LF,~":)"1@#~([:*/2=/\[:,2 2+/@,;._3])"2)(3 3)($"1)1+!A.&i.])@9:
   $ f ''  NB. A function has to take something to be invoked,
           NB. but in this case it is not used by the function
   37 {. f ''  NB. Take the first 37 characters
1 5 3
9 8 7
4 2 6

1 5 6
8 7 3
4 2 9

   _38 {. f ''  NB. Take the last 38 characters
9 5 4
2 3 7
6 8 1

9 5 7
1 2 3
6 8 4


   NB. The output string ends with two newlines
mile
źródło