Post by danmaz on Jan 7, 2019 17:28:23 GMT
Hi guys
A magic square is a square filled with numbers (in this case 4x4 = 16) in such a way that you obtain the same
sum of numbers along any rows, columns or diagonals
As follows one of the simplest code on internet to generate 4x4 magic squares by filling it with numbers (from 1 to 16)
In total you will display all 880 combinations (excluded are rotations and mirror images).
A file is open for output , just to grasp all of them
Enjoy....comment are welcome
Daniele
A magic square is a square filled with numbers (in this case 4x4 = 16) in such a way that you obtain the same
sum of numbers along any rows, columns or diagonals
As follows one of the simplest code on internet to generate 4x4 magic squares by filling it with numbers (from 1 to 16)
In total you will display all 880 combinations (excluded are rotations and mirror images).
A file is open for output , just to grasp all of them
Enjoy....comment are welcome
Daniele
' ****** MAGIC SQUARES 4x4 SOLVER ****************
' * *
' * FREEWARE by danmaz *
' * mazzad50@gmail.com www.molecularmodels.eu *
' * *
' **************************************************
' A magic square is a n times n square grid (where n is the number of cells on each side) filled with positive
' integers in the range 1 to n^2 such that each cell contains a different integer and the sum of the integers in each row
' column and diagonal is equal. This sum is called the magic constant or magic sum of the magic square.
' Here we generate the 880 magic squares of order 4 (magic sum is 34) discarding those obtained by reflections and rotations.
GLOBAL q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15, q16, nTot
mainwin 64 24
locate 10,4: print "+-----+-----+-----+-----+" '+-------------------+
locate 10,5: print "| MAGIC SQUARE 4 X 4 |" '| q1 | q2 | q3 | q4 |
locate 10,6: print "+-----+-----+-----+-----+" '+-------------------+
locate 10,7: print "| GENERATOR |" '| q5 | q6 | q7 | q8 |
locate 10,8: print "+-----+-----+-----+-----+" '+-------------------+
locate 10,9: print "| | | | |" '| q9 | q10| q11| q12|
locate 10,10:print "+-----+-----+-----+-----+" '+-------------------+
locate 10,11:print "| << please wait ! >> |" '| q13| q14| q15| q16|
locate 10,12:print "+-----+-----+-----+-----+" '+-------------------+
locate 4,16:print "magic square number = "
locate 8,1:print "**** MAGIC SQUARES 4x4 SOLVER ****"
locate 14,2:print "Freeware by danmaz JustBASIC v2.0"
locate 4,18:print "Look at the complete output in MagicSquares.txt"
' q1....q16 are the actual values of each element of the square, as shown above
' s1....s16 are indeces that vary acoording to the variuos attemps to find q1...q16 values
' they refer to the position inside g16$....g1$
OPEN "MagicSquares.txt" for output as #1
FOR i = 1 TO 16:g16$ = g16$ + CHR$(i):NEXT i ' start strig g16$ is generated in this line
nTot = 0 ' total number of squares is set to 0
' in the following 8 lines we impose some limits to the corner indeces
' in order to avoid magic squares obtained by rotation or mirroring of other existing
' q1 must be lowest value among (q1,q4,q13,q16), so cannot exceed 13
FOR s1 = 1 TO 13
FOR s4 = s1 + 1 TO 16
FOR s13 = s1 + 1 TO 16
FOR s16 = s1 + 1 TO 16
IF s1 = s4 OR s1 = s13 OR s1 = s16 OR s4 = s13 OR s4 = s16 OR s13 = s16 THEN 1000
IF s4 > s13 THEN 1000 ' il vertice q4 deve essere minore di q13
q1 = s1: q4 = s4: q13 = s13: q16 = s16
g12$ = ""
' here we produce g12$, which is then used to produce g11$...g10$... and so on
' it represents the elements still available (not present on the 4x4 square)
FOR i = 1 TO 16
g1$ = MID$(g16$, i, 1)
IF i = s1 OR i = s4 OR i = s13 OR i = s16 THEN 100
g12$ = g12$ + g1$
100 NEXT i
' now we start the real job!
FOR s6 = 1 TO 12 ' position 6 chosen by iterative cycle
q6 = ASC(MID$(g12$, s6, 1))
g11$ = LEFT$(g12$, s6 - 1) + MID$(g12$, s6 + 1)
q11 = 34 - q1 - q6 - q16 ' position 11 calculated
IF q11 < 1 OR q11 > 16 THEN 980
z = INSTR(g11$, CHR$(q11)): IF z = 0 THEN 980
g10$ = LEFT$(g11$, z - 1) + MID$(g11$, z + 1)
FOR s10 = 1 TO 10 ' position 10 chosen by iterative cycle
q10 = ASC(MID$(g10$, s10, 1))
g9$ = LEFT$(g10$, s10 - 1) + MID$(g10$, s10 + 1)
q7 = 34 - q4 - q10 - q13 ' position 7 calculated
IF q7 < 1 OR q7 > 16 THEN 970
z = INSTR(g9$, CHR$(q7)): IF z = 0 THEN 970
g8$ = LEFT$(g9$, z - 1) + MID$(g9$, z + 1)
FOR s2 = 1 TO 8 ' position 2 chosen by iterative cycle
q2 = ASC(MID$(g8$, s2, 1))
g7$ = LEFT$(g8$, s2 - 1) + MID$(g8$, s2 + 1)
q3 = 34 - q1 - q2 - q4 ' position 3 calculated
IF q3 < 1 OR q3 > 16 THEN 960
z = INSTR(g7$, CHR$(q3)): IF z = 0 THEN 960
g6$ = LEFT$(g7$, z - 1) + MID$(g7$, z + 1)
q14 = 34 - q2 - q6 - q10 ' position 14 calculated
IF q14 < 1 OR q14 > 16 THEN 960
z = INSTR(g6$, CHR$(q14)): IF z = 0 THEN 960
g5$ = LEFT$(g6$, z - 1) + MID$(g6$, z + 1)
q15 = 34 - q3 - q7 - q11 ' position 15 calculated
IF q15 < 1 OR q15 > 16 THEN 960
z = INSTR(g5$, CHR$(q15)): IF z = 0 THEN 960
g4$ = LEFT$(g5$, z - 1) + MID$(g5$, z + 1)
FOR s5 = 1 TO 4 ' position 5 chosen by iterative cycle
q5 = ASC(MID$(g4$, s5, 1))
g3$ = LEFT$(g4$, s5 - 1) + MID$(g4$, s5 + 1)
q9 = 34 - q1 - q5 - q13 ' position 9 calculated
IF q9 < 1 OR q9 > 16 THEN 950
z = INSTR(g3$, CHR$(q9)): IF z = 0 THEN 950
g2$ = LEFT$(g3$, z - 1) + MID$(g3$, z + 1)
q8 = 34 - q5 - q6 - q7 ' position 8 calculated
IF q8 < 1 OR q8 > 16 THEN 950
z = INSTR(g2$, CHR$(q8)): IF z = 0 THEN 950
g1$ = LEFT$(g2$, z - 1) + MID$(g2$, z + 1)
q12 = 34 - q9 - q10 - q11 ' position 12 calculated
IF q12 < 1 OR q12 > 16 THEN 950
IF ASC(g1$) <> q12 THEN 950
if zc = 0 then call prepareTable:zc = zc + 1
nTot = nTot + 1: LOCATE 28,16 : PRINT nTot;
CALL writeIt ' we reached a regular magic square, so we write it down
950 NEXT s5
960 NEXT s2
970 NEXT s10
980 NEXT s6
1000 NEXT s16:NEXT s13:NEXT s4:NEXT s1
PRINT " --> Job finished !! ":close #1
End
'-------------- sub's ----------------
Sub prepareTable
locate 10,4: print "+-----+-----+-----+-----+"
locate 10,5: print "| | | | |"
locate 10,6: print "+-----+-----+-----+-----+"
locate 10,7: print "| | | | |"
locate 10,8: print "+-----+-----+-----+-----+"
locate 10,9: print "| | | | |"
locate 10,10:print "+-----+-----+-----+-----+"
locate 10,11:print "| | | | |"
locate 10,12:print "+-----+-----+-----+-----+"
End Sub
Sub writeIt
locate 12,5:print q1;" ":locate 18,5:print q2;" ":locate 24,5:print q3;" ":locate 30,5:print q4;" "
locate 12,7:print q5;" ":locate 18,7:print q6;" ":locate 24,7:print q7;" ":locate 30,7:print q8;" "
locate 12,9:print q9;" ":locate 18,9:print q10;" ":locate 24,9:print q11;" ":locate 30,9:print q12;" "
locate 12,11:print q13;" ":locate 18,11:print q14;" ":locate 24,11:print q15;" ":locate 30,11:print q16;" "
print #1,"(";nTot;")"
print #1,q1;" ";q2;" ";q3;" ";q4
print #1,q5;" ";q6;" ";q7;" ";q8
print #1,q9;" ";q10;" ";q11;" ";q12
print #1,q13;" ";q14;" ";q15;" ";q16
print #1,"-------------------"
End Sub