This is solved by brute force. This is qbasic code, and it compiles fine using #lang "qb" header. It took 57 seconds to solve on my PC.

Code: Select all

```
#lang "qb"
Dim As integer SquareA,SquareB,SquareC,SquareD,SquareE
Dim As integer SquareF,SquareG,SquareH,SquareI
SquareA = 1: SquareB = 2: SquareC = 3
SquareD = 4: SquareE = 5: SquareF = 6
SquareG = 7: SquareH = 8: SquareI = 9
SquareA = 1: SquareB = 2: SquareC = 3
SquareD = 4: SquareE = 5: SquareF = 6
SquareG = 7: SquareH = 8: SquareI = 9
GOSUB DoScreen
GOSUB FillSquare
Time1$ = TIME$
DO
GOSUB Add1
GOSUB Validate
LOOP WHILE INKEY$ = ""
End
DoScreen:
COLOR 14,0
CLS
LOCATE 5, 29: PRINT "######################"
LOCATE 6, 29: PRINT "# #"
LOCATE 7, 29: PRINT "# ############# #"
LOCATE 8, 29: PRINT "# # # # # #"
LOCATE 9, 29: PRINT "# ############# #"
LOCATE 10, 29: PRINT "# # # # # #"
LOCATE 11, 29: PRINT "# ############# #"
LOCATE 12, 29: PRINT "# # # # # #"
LOCATE 13, 29: PRINT "# ############# #"
LOCATE 14, 29: PRINT "# #"
LOCATE 15, 29: PRINT "######################"
RETURN
Add1: 'Loop through iterations
SquareI = SquareI + 1
IF SquareI > 9 THEN SquareI = 1: SquareH = SquareH + 1
IF SquareH > 9 THEN SquareH = 1: SquareG = SquareG + 1
IF SquareG > 9 THEN SquareG = 1: SquareF = SquareF + 1
IF SquareF > 9 THEN SquareF = 1: SquareE = SquareE + 1
IF SquareE > 9 THEN SquareE = 1: SquareD = SquareD + 1
IF SquareD > 9 THEN SquareD = 1: SquareC = SquareC + 1
IF SquareC > 9 THEN SquareC = 1: SquareB = SquareB + 1
IF SquareB > 9 THEN SquareB = 1: SquareA = SquareA + 1
IF SquareA > 9 THEN SquareA = 1
RETURN
Validate: 'validate current iteration ( no repeated Squares )
IF SquareI = SquareH THEN RETURN
IF SquareI = SquareG THEN RETURN
IF SquareI = SquareF THEN RETURN
IF SquareI = SquareE THEN RETURN
IF SquareI = SquareD THEN RETURN
IF SquareI = SquareC THEN RETURN
IF SquareI = SquareB THEN RETURN
IF SquareI = SquareA THEN RETURN
IF SquareH = SquareG THEN RETURN
IF SquareH = SquareF THEN RETURN
IF SquareH = SquareE THEN RETURN
IF SquareH = SquareD THEN RETURN
IF SquareH = SquareC THEN RETURN
IF SquareH = SquareB THEN RETURN
IF SquareH = SquareA THEN RETURN
IF SquareG = SquareF THEN RETURN
IF SquareG = SquareE THEN RETURN
IF SquareG = SquareD THEN RETURN
IF SquareG = SquareC THEN RETURN
IF SquareG = SquareB THEN RETURN
IF SquareG = SquareA THEN RETURN
IF SquareF = SquareE THEN RETURN
IF SquareF = SquareD THEN RETURN
IF SquareF = SquareC THEN RETURN
IF SquareF = SquareB THEN RETURN
IF SquareF = SquareA THEN RETURN
IF SquareE = SquareD THEN RETURN
IF SquareE = SquareC THEN RETURN
IF SquareE = SquareB THEN RETURN
IF SquareE = SquareA THEN RETURN
IF SquareD = SquareC THEN RETURN
IF SquareD = SquareB THEN RETURN
IF SquareD = SquareA THEN RETURN
IF SquareC = SquareB THEN RETURN
IF SquareC = SquareA THEN RETURN
IF SquareB = SquareA THEN RETURN
Permutations& = Permutations& + 1
GOSUB FillSquare
RETURN
FillSquare:
COLOR 11
LOCATE 8, 34: PRINT SquareA: LOCATE 8, 38: PRINT SquareB: LOCATE 8, 42: PRINT SquareC
LOCATE 10, 34: PRINT SquareD: LOCATE 10, 38: PRINT SquareE: LOCATE 10, 42: PRINT SquareF
LOCATE 12, 34: PRINT SquareG: LOCATE 12, 38: PRINT SquareH: LOCATE 12, 42: PRINT SquareI
Sum1 = SquareA + SquareB + SquareC
Sum2 = SquareD + SquareE + SquareF
Sum3 = SquareG + SquareH + SquareI
Sum4 = SquareA + SquareD + SquareG
Sum5 = SquareB + SquareE + SquareH
Sum6 = SquareC + SquareF + SquareI
Sum7 = SquareA + SquareE + SquareI
Sum8 = SquareC + SquareE + SquareG
COLOR 15
LOCATE 8, 46: PRINT Sum1
LOCATE 10, 46: PRINT Sum2
LOCATE 12, 46: PRINT Sum3
LOCATE 14, 33: PRINT Sum4
LOCATE 14, 37: PRINT Sum5
LOCATE 14, 41: PRINT Sum6
LOCATE 6, 46: PRINT Sum8
LOCATE 14, 46: PRINT Sum7
GOSUB TestSums
RETURN
TestSums: 'test sums
IF Sum1 <> Sum2 THEN RETURN
IF Sum2 <> Sum3 THEN RETURN
IF Sum3 <> Sum4 THEN RETURN
IF Sum4 <> Sum5 THEN RETURN
IF Sum5 <> Sum6 THEN RETURN
IF Sum6 <> Sum7 THEN RETURN
IF Sum7 <> Sum8 THEN RETURN
Time2$ = TIME$
COLOR 12
LOCATE 17, 29: PRINT " Puzzle solved! " 'print answer
LOCATE 18, 29: PRINT " Permutations: ": LOCATE 18, 43: PRINT Permutations&
LOCATE 19, 29: PRINT " Start time: "; Time1$; " "
LOCATE 20, 29: PRINT " End time: "; Time2$; " "
DO: LOOP WHILE INKEY$ = "": SYSTEM
RETURN
```