Solving Sudoku Using Monte Carlo Marching
Solving Sudoku Using Monte Carlo Marching
Marching
Jacob H Lashover
September 17, 2015
Baton Rouge, Louisiana
Abstract: As noted in an earlier paper titled Monte Carlo Marching, the author
had previously solved Sudoku puzzles using Monte Carlo Marching (MCM).
(Lashover, 2012) In this paper, details of that MCM solution are reported. The
MCM algorithm is shown to provide exact, integer solutions to 6 X 6 Sudoku
puzzles. A math model is constructed: a convex objective function is developed
from linear constraints, non-linear constraints, and non-smooth constraints. The
linearized objective function is then minimized to zero using MCM which
determines the values of the unknown variables (as many as 19). This MCM
method is unique in that no other analytical solution has been found in the
literature.
The MCM analytical method has now been shown to solve chemical
engineering problems such as calculation of dew points and calculation of fractions
of vapor flashed in chemical streams. These calculations required solution of nonlinear, simultaneous equations. Rufus Isaacs' non-linear model for optimizing
horse racing wagers was also solved in "A Chemical Engineer Goes to the Horse
Races". (Lashover J. H., 2014) These algorithms demonstrate that whether the
equations are linear, non-linear, or non-smooth, MCM, which does not require
derivatives, is a very powerful solution method.
Key Words: Algorithm, analytical solution, constraints, mathematical method,
optimization, mathematical modeling, Sudoku, QB64 computer language,
convergence, non-linear equations, convex objective function, chemical
engineering, and horse racing wagers.
Table of Contents
Page No.
1
2 - 4
Content
Abstract and Key Words
Table of Contents
Introduction
Discussion
A. Development of Math Model
10
11
12
13
2
14
16
17
18
19
20
21
22
23
End of Appendix A.
24
25
25 32
33
33 continued
34
35
36 63
64
Bibliography
Introduction:
Sudoku is played on a square matrix, in this case, 6 x 6 or 36 total squares. The
same single integer cannot appear twice in the same row, column or in any of the
six 2 x 3 sub-matrices (called grids in this work). Clues which consist of at least 8
permanent numbers located throughout the total matrix are provided. For a puzzle
to be a true Sudoku puzzle, it can only have one solution. The puzzles are
traditionally solved using logic, and the difficulty of a puzzle is determined by the
depth of logic required.
Sudokus origins lie in a Chinese game called Magic Squares which was first
documented 2000 years ago. All of the rows, columns, and diagonals had to add
to the same number. Numbers could only be used once. In 1514, Albrecht Durer,
a German artist, produced a 4 X 4 game called Melancholia where the sum of all
rows, columns, and diagonals had to total 34. Again, numbers could only be used
once. In 1783, the Swiss mathematician, Leonhard Euler, wrote about a game he
called Latin Squares where he removed the diagonal requirements and made the
game a puzzle of permutations. There are 6! = 720 different ways that 6
numbers can be arranged. Between 1890 and 1920, a game called Magic
Squares appeared in a French newspaper. The game first appeared in 1978 in the
U.S., and in 1984 in Japan where it was given the name Sudoku which is short
for a longer expression which means the digits are limited to one occurrence.
There are many variations of Sudoku including matrices of 4 X 4, 6 X 6, 9 X 9,
etc. and sometimes adding additional constraint(s) by including the diagonal(s) of
the matrix (X-Sudoku). This paper presents an analytical solution for 6 X 6
puzzles with six 2 X 3 grids. Three example puzzles with 12, 16, and 13
permanent clues, respectively, are solved. See page 4 for puzzle No. 1. See
Appendix B for puzzles No. 2 and No. 3. While fewer clues generally make a
puzzle more difficult to solve, the ultimate difficulty is presently agreed upon by
most experts to be the number and complexity of the solving techniques required
to find a solution. These techniques include, hidden singles, hidden pairs, hidden
triples, hidden quads, naked singles, naked pairs, naked triples, naked quads, XWing, Swordfish, XY-Wing, etc.
A naked single occurs when only one number can fit the constraints of one
number per row, column, and grid for a square. That naked single is an obvious
solution. A hidden single occurs when only one of many possible solution
numbers appears in the list of possible solutions for a row, column, or grid.
5
Naked and Hidden Singles are initially searched. Finding these solutions
permits rechecking for newly uncovered singles after each find. This is called
propagation of solutions which may significantly reduce the number of
unknowns and hence computing time. This procedure found 8, 4, and 4 additional
clues for Puzzles 1, 2, and 3, respectively. This left 16, 16, and 19 unidentified
squares in Puzzles 1, 2, and 3, respectively.
The computer used to solve these puzzles was a Dell Inspiron 546 Desktop
with 6 GB of RAM, and an AMD Phenom 9650 Quad-Core Processor operating
at 2.30 GHz and using QB64. Times in seconds required for solution of the three
puzzles were 30, 94, and 108, respectively.
Discussion
A. Development of Math Model
Puzzle No. 1 shown below in Figure 1 was randomly selected from those found in
an internet search. It has 12 clues or known squares out of 36 total squares. These
clues are highlighted in yellow. Solution by MCM makes use of constraints which
insure that the solution will only have one of each of the six possible digits in each
row, column, and grid; and no duplicates. The sum and product of the six digits in
each row, column, and grid must be 21 and 720, respectively. These two
constraints form simultaneous linear and non-linear equations which model the
system being studied. These equations have associative solutions as the sums and
products can equal 21 and 720 regardless of their positions in the square. Third
and fourth constraints limit the total number of each digit to six in each set of rows,
columns, and grids; and insure that there are no duplicates. The MCM objective
function is formed by designing the constraints to be convex and adding them. The
justification for the addition of these constraints is similar to that used in
Maximum Likelihood methods. (Banbura, 2010) The objective function
becomes convex by being built of convex functions and can thus be minimized.
The feasible solutions are bounded from 1 to 6. Further proof of the linearity
(ability to add constraints) of the simultaneous marching schema used in this
work can be found in Analysis of the n-dimensional quadtree decomposition for
arbitrary hyper-rectangles by Cristos Faloutsos et.al.at
http://drum.lib.umd.edu/bitstream/1903/678/2/CS-TR-3381.pdf.
The model is described by assigning X(k) sequentially to the squares from X(1) to
X(36). The squares are also described in two dimensions by A(Row, Col) where
A(1, 1) = X(1) on down to A(6, 6) = X(36). Having these two nomenclatures
facilitates referring to the grid variables. Short subroutines can be called to convert
k to (Row, Col) and (Row, Col) to k. Grids 1 to 6 are 2 rows and 3 columns
numbered from left to right and top to bottom.
1
2
3
4
5
6
2
6
4
1
5
3
1
5
1
2
3
4
6
2
FIGURE 1.
4
3
6
5
1
2
3
6
5
1
2
3
4
4
3
2
5
4
6
1
5
1
4
3
6
2
5
6
Row #2.
i=7 to 12 Xi = 21
Row #3
i=13 to 18 Xi = 21
Row #4.
i=19 to 24 Xi = 21
Row #5.
i=25 to 30 Xi = 21
Row #6.
i=31 to 36 Xi = 21
Column #2.
Column #3.
Column #4.
Column #5.
Column #6.
Grid #2.
Grid #3.
Grid #4.
Grid #5.
Grid #6.
Row #2.
i = 7 to 12 Xi =720
Row #3.
i = 13 to 18 Xi =720
Row #4.
i = 19 to 24 Xi =720
Row #5.
i = 25 to 30 Xi =720
Row #6.
i = 31 to 36 Xi =720
Column #2.
Column #3.
Column #4.
Column #5.
Column #6.
Grid #2.
Grid #3.
Grid #4.
Grid #5.
Grid #6.
Constraint #3: Total of each digit equal to 6 in each grid, row, and column where TGD(Grids,
Digit), TRD(Rows, Digits), TCD(Columns, Digits) represent the total count of each digit in
grids, rows, and columns, respectively. Details of how these values are obtained follow in
Section A. TOTONES% = total number of ones, TOTTWOS% = total number of twos etc.
TOTONES% = 0: TOTTWOS% = 0: TOTTHREES% = 0: TOTFOURS% = 0:
TOTFIVES% = 0: TOTSIXES% = 0
FOR II = 1 TO 6: JJ = 1: TOTONES% = TOTONES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II:
T1 = ABS(TOTONES% - 18)
FOR II = 1 TO 6: JJ = 2: TOTTWOS% = TOTTWOS% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II:
T2 = ABS(TOTTWOS% - 18)
FOR II = 1 TO 6: JJ = 3: TOTTHREES% = TOTTHREES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II:
T3 = ABS(TOTTHREES% - 18)
FOR II = 1 TO 6: JJ = 4: TOTFOURS% = TOTFOURS% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II:
T4 = ABS(TOTFOURS% - 18)
FOR II = 1 TO 6: JJ = 5: TOTFIVES% = TOTFIVES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II:
T5 = ABS(TOTFIVES% - 18)
FOR II = 1 TO 6: JJ = 6: TOTSIXES% = TOTSIXES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II:
T6 = ABS(TOTSIXES% - 18)
Constraint #4. Total duplicates in the grids, TOTSUMGD, rows, TOTSUMRD, and columns,
TOTSUMCD, are added to determine the total duplicates. Details are in Section B below, and
Appendix A.
10
11
Figure 2. Computer output for all rows with details for Row #1
The count of the number of digits, 1 to 6, plus the number of duplicates can be
determined from the above results. The code for columns and grids is similar and
can be found in Appendix A.
C. Solution of the model
Lower (LL) and Upper (UL) limits or bounds are set for the puzzle variables at 1
and 6. The differences between these limits are called the ranges of the variables
for each square and can be different for each square. A Crosshatch subroutine
(eliminates digits which are already in use) is used to check for the set of feasible
values for each unknown square. In the inner loop, I, values of X(k) for each
iteration are chosen randomly from these sets of feasible variables for each square.
The convex constraint equations are evaluated for the set of X(k)s selected and
inserted into the objective function. If the values chosen provide the minimum
value of the objective function so far, they are retained as the best solution to date.
If not, another similar iteration is performed.
A predetermined number of iterations is executed before the second loop, J, is
entered. This loop raises the lower bounds, LL, of the hyper-rectangles for each
X(k) toward the upper bounds, UL, based on the best solution of each so far. The
range, R, of each hyper-rectangle is individually reduced to center its limits around
the best solution. A focus factor, H, is used in the calculations to reduce the
12
rectangle widths by a power. When the power is two (2) the range is bisected each
time. The equation describing the rectangle area is f(x) = R/(HJ) where R is the
range of solutions and H=1.3 with J = 1, 2, 3,.n, the number of the outer loop
iteration. In the case of bisection, where H =2, the first iteration when J = 1 splits
the range in half. In the second iteration where J = 2, the remaining range is
reduced by 2^2 or one fourth. The limit of this function as J approaches infinity is
0 where the entire range of solutions has been examined.
The three puzzles chosen for this report are Easy, Medium Hard and Hard.
The focus factors for each puzzle were chosen from experience to be 1.3. The
number of iterations in the inner loop, I, was increased as the difficulty of the
puzzle increased. Values of 5000, 30,000, and 40,000 iterations of I were used,
respectively, as the puzzle difficulty increased. Completion times for the three
puzzles were 30, 94, and 108 seconds, respectively, with no pauses. Removing the
comment code () on the Input Press ENTER to continue, Yes will permit the
user to watch the algorithm move the objective function values to zero.
CLS
'PRINT: PRINT " PTT= "; PTT; " CTT= "; CTT; " GT= "; GT
'PRINT: PRINT " PMTT= "; PMTT; " CMTT= "; CMTT; " GMT= "; GMT
'PRINT: PRINT " TOTSUMGD= "; TOTSUMGD; " TOTSUMRD= "; TOTSUMRD; "
TOTSUMCD= "; TOTSUMCD
'PRINT: PRINT " T1= "; T1; " T2= "; T2; " T3= "; T3; " T4= "; T4; " T5= "; T5; " T6= "; T6
COUNTI& = 0
COUNTJ& = COUNTJ& + 1: PRINT: PRINT " COUNTJ= "; COUNTJ&
'INPUT " PRESS ENTER TO CONTINUE", YES
555 NEXT J
556 CLS: PRINT: PRINT " FINAL RESULTS!"
PRINT: PRINT " S= "; S; " MM= "; MM
PRINT: PRINT " J= "; J; " I= "; I&; " S= "; S; " MM= "; MM
PRINT: PRINT " COUNTJ = "; COUNTJ&; " COUNTI= "; COUNTI&
BEEP: BEEP
TF# = TIMER
15
PRINT: PRINT " START TIME= "; TS#; " FINISH TIME= "; TF#
ELAPSE# = (TF# - TS#): PRINT " ELAPSED TIME= "; ELAPSE#; " SECONDS"
INPUT "PRESS ENTER TO CONTINUE", YES
GOSUB 190 Print solution to computer screen
GOSUB 200 Print solution to hard drive
INPUT "PRESS ENTER TO CONTINUE", YES
GOTO 999 END
16
END IF
NEXT II
NEXT GRID%
18
P6 = ABS(P6 - 21)
PRINT: PRINT " PM6= "; PM6; " P6= "; P6
PT = P1 + P2 + P3 + P4 + P5 + P6
PMT = PM1 + PM2 + PM3 + PM4 + PM5 + PM6
PRINT: PRINT " PT= "; PT; " PMT= "; PMT
REM GRID% #6
G6 = X(28) + X(29) + X(30) + X(34) + X(35) + X(36)
GM6 = X(28) * X(29) * X(30) * X(34) * X(35) * X(36)
PRINT " GM6= "; GM6
GM6 = ABS(GM6 - 720)
G6 = ABS(G6 - 21)
PRINT: PRINT " GM6= "; GM6; " G6= "; G6
GT = G1 + G2 + G3 + G4 + G5 + G6
GMT = GM1 + GM2 + GM3 + GM4 + GM5 + GM6
PRINT: PRINT " GT= "; GT; " GMT= "; GMT
REM COUNT TOTAL DIGITS OF EACH KIND IN GRIDS, ROWS AND COLUMNS AND
CONVERT TO CONVEX FORM
TOTONES% = 0: TOTTWOS% = 0: TOTTHREES% = 0: TOTFOURS% = 0:
TOTFIVES% = 0: TOTSIXES% = 0
FOR II = 1 TO 6: JJ = 1: TOTONES% = TOTONES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II: T1 = ABS(TOTONES% - 18) The same six digits appear in each unit
FOR II = 1 TO 6: JJ = 2: TOTTWOS% = TOTTWOS% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II: T2 = ABS(TOTTWOS% - 18)
FOR II = 1 TO 6: JJ = 3: TOTTHREES% = TOTTHREES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II: T3 = ABS(TOTTHREES% - 18)
FOR II = 1 TO 6: JJ = 4: TOTFOURS% = TOTFOURS% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II: T4 = ABS(TOTFOURS% - 18)
FOR II = 1 TO 6: JJ = 5: TOTFIVES% = TOTFIVES% + TGD(II, JJ) + TRD(II, JJ) +
TCD(II, JJ): NEXT II: T5 = ABS(TOTFIVES% - 18)
22
23
1
2
3
4
5
6
1
2
3
4
5
6
24
6
1
3
4
2
5
1
3
1
6
5
4
2
1
2
5
6
1
3
4
2
FIGURE 3
3
4
2
5
6
1
3
4
3
5
6
1
2
4
5
6
1
2
4
3
5
1
2
4
3
5
6
6
4
5
2
3
6
1
2
FIGURE 4.
2
6
1
4
3
5
3
5
4
3
2
1
6
4
1
2
4
6
5
3
5
6
3
5
1
2
4
6
PTT=
14
PMTT=
CTT=
1634
TOTSUMGD=
T1=
S=
5168
16
GT=
CMTT=
16
1920
T2=
MM=
T3=
PTT=
CTT=
T1=
1542
25
TOTSUMCD=
T4=
5168
MM=
5168
GT=
CMTT=
10
T2=
16
1486
T5=
16
T6=
12
5168
TOTSUMGD=
GMT=
TOTSUMRD=
J= 1 I= 1 S=
2 5 3 5 5 1
PMTT=
14
900
TOTSUMRD=
GMT=
T3=
868
16
T4=
TOTSUMCD=
6
T5=
8
T6=
S=
3371
MM=
3371
J= 1 I= 2 S=
2 5 4 5 3 1
6
PTT=
CTT=
3371
PMTT=
1200
TOTSUMGD=
T1=
S=
2965
GT=
CMTT=
T2=
MM=
PTT=
CTT=
1800
S=
2734
J=
26
840
TOTSUMCD=
T4=
2965
GT=
CMTT=
10
T5=
10
T6=
MM=
272
47
T3=
MM=
2965
GMT=
592
TOTSUMRD=
T2=
I=
GMT=
2965
T1=
860
T3=
S=
1
TOTSUMGD=
3371
TOTSUMRD=
J= 1 I= 10
2 6 4 6 4
PMTT=
MM=
14
T4=
TOTSUMCD=
12
T5=
2734
S=
2734
MM=
2734
10
T6=
PTT=
CTT=
PMTT=
668
CMTT=
TOTSUMGD=
T1=
S=
2131
GT=
8
0
T3=
MM=
S=
1
PTT=
CTT=
636
S=
1760
27
10
T6=
T5=
GT=
MM=
2131
GMT=
552
528
TOTSUMRD=
T2=
MM=
TOTSUMCD=
T4=
2131
T3=
10
T6=
T4=
TOTSUMCD=
T5=
1760
J= 1 I= 221 S=
2 5 4 5 4 1
6
416
12
CMTT=
TOTSUMGD=
GMT=
2131
J= 1 I= 93
2 3 4 5 5
T1=
984
TOTSUMRD=
T2=
PMTT=
1760
MM=
1760
PTT=
CTT=
PMTT=
452
CMTT=
TOTSUMGD=
T1=
S=
1752
GT=
624
T3=
MM=
PTT=
CTT=
T1=
S=
1563
1752
GT=
CMTT=
T6=
T5=
MM=
GMT=
TOTSUMRD=
T2=
MM=
T3=
1752
636
380
TOTSUMCD=
T4=
T5=
1563
J= 1 I= 2548 S=
2 3 4 6 5 1
5
28
T4=
TOTSUMCD=
1752
500
624
12
J= 1 I= 474 S=
2 5 4 4 5 1
TOTSUMGD=
GMT=
TOTSUMRD=
T2=
PMTT=
1563
MM=
1563
8
T6=
PTT=
CTT=
PMTT=
414
CMTT=
TOTSUMGD=
T1=
S=
1539
GT=
408
TOTSUMRD=
T2=
MM=
T3=
PTT=
CTT=
T1=
S=
1528
GT=
10
T5=
T6=
T2=
912
MM=
T3=
MM=
1539
2
GMT=
TOTSUMRD=
6
10
272
T4=
TOTSUMCD=
15
T5=
1528
J= 2 I= 194 S=
2 5 4 6 3 1
4
29
T4=
1539
CMTT=
TOTSUMGD=
TOTSUMCD=
1539
272
684
J= 1 I= 3647 S=
2 6 3 4 4 1
PMTT=
GMT=
1528
MM=
1528
12
T6=
PTT=
PMTT=
CTT=
480
S=
1476
GT=
CMTT=
TOTSUMGD=
T1=
480
T3=
MM=
PTT=
CTT=
T1=
S=
1453
GT=
CMTT=
2
0
T5=
T6=
MM=
1476
624
T3=
GMT=
6
240
TOTSUMCD=
T4=
T5=
1453
PTT=
CTT=
30
MM=
TOTSUMRD=
T2=
368
T4=
1476
J= 2 I= 680 S=
2 5 3 6 4 1
PMTT=
TOTSUMCD=
1476
564
480
J= 2 I= 281 S=
2 5 3 6 5 1
TOTSUMGD=
GMT=
TOTSUMRD=
T2=
PMTT=
CMTT=
1453
GT=
248
MM=
1453
4
GMT=
568
4
T6=
TOTSUMGD=
T1=
S=
1246
10
T2=
TOTSUMRD=
0
MM=
T3=
PTT=
CTT=
588
T1=
S=
1190
1246
GT=
CMTT=
TOTSUMGD=
T2=
288
T3=
MM=
PTT=
CTT=
T1=
31
T6=
1246
GMT=
288
TOTSUMCD=
T4=
1190
GT=
CMTT=
6
T2=
T5=
T5=
2
T6=
1190
TOTSUMGD=
MM=
TOTSUMRD=
308
12
10
J= 2 I= 1672 S=
2 5 4 5 4 1
PMTT=
T4=
TOTSUMCD=
1246
J= 2 I= 1068 S=
2 5 4 5 3 1
PMTT=
10
168
T3=
1190
TOTSUMRD=
0
MM=
GMT=
10
T4=
168
TOTSUMCD=
T6=
T5=
S=
681
MM=
681
J= 3 I= 1827 S=
2 5 4 5 3 1
6
PTT=
CTT=
PMTT=
264
TOTSUMGD=
T1=
S=
270
J=
MM=
GT=
GMT=
CMTT=
0
TOTSUMRD=
T2=
MM=
I=
681
T3=
TOTSUMCD=
T4=
T5=
270
2801
S=
270
SOLUTION ACHIEVED! I=
MM=
124
QMCMSU27.BAS
PUZZLE#1
09-17-2015
20:48:23
2 5 4 6 3 1
6
ELAPSED TIME=
32
681
14.5
SECONDS
J=
270
5
0
T6=
S
5168
3371
2965
2734
2131
1760
1752
1563
1539
1528
1476
1453
1246
1190
J
1
1
1
1
1
1
1
1
1
2
2
2
2
2
I
1
2
10
47
93
221
474
2548
3647
194
281
680
1068
1672
681
270
0
3
3
5
1827
2801
124
S-Blue
JOrange
I-Gray
33
14191
13127
8225
6920
4570
4452
4172
3263
3084
2775
1534
1154
1101
920
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
3
4
10
14
20
21
75
103
271
1090
2765
16581
7242
658
555
540
375
270
0
2
3
3
4
4
4
17211
2621
18244
1939
22302
24458
S vs J & I
30000
25000
20000
15000
S-Blue
10000
5000
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Series1
JOrange
Series2
Series3
I-Gray
34
S
44122
8819
5111
4829
3507
2749
2626
2196
2081
1902
1854
1818
1587
1526
1519
1458
1378
1309
1110
1094
790
447
0
J
1
1
1
1
1
1
1
1
2
2
3
3
3
3
3
3
3
3
3
3
3
4
4
I
1
2
7
338
653
2200
3140
6211
2597
26269
588
2062
3422
5514
6102
17536
18486
19265
21610
25745
37194
1995
2587
S vs J & I
50000
40000
30000
20000
S-Blue
10000
0
1
5
Series1
JOrange
11 13 15 17 19 21 23
Series2
Series3
I-Gray
35
DIM X(36), L(36), Y(36), A(6, 6), Z(36), AA(36), LL(36), UL(36), CTG(6, 6), CTC(6, 6),
CTR(6, 6) AS LONG
DIM ZX(6, 6), SUMGD(6), SUMRD(6), SUMCD(6), TGD(6, 6), TRD(6, 6), TCD(6, 6), P(6),
PM(6), C(6), CM(6) AS LONG
DIM YY$(6), XY$(6), XX$(6), YYY$(6, 6)
DIM XXY(6, 6), YYX(6, 6) AS LONG
DIM CT(6, 6), NF(6, 6) AS LONG
36
ON OP2 GOTO 1, 2, 3
GOTO 36
GOTO 36
GOTO 36
FOR COL% = 1 TO 6
GOSUB 40 'CONVERT ROW% AND COL% TO K
UL(K) = 6: LL(K) = 1: AA(K) = 3
IF ZX(ROW%, COL%) <> 0 THEN X(K) = ZX(ROW%, COL%): GOTO 71
GOTO 68
71 AA(K) = X(K): Z(K) = X(K): A(ROW%, COL%) = X(K)
68 NEXT COL%
70 NEXT ROW%
MM = 1E+30: H = 1.3
RANDOMIZE (-1111)
COUNTI& = 0: COUNT& = 0
TS# = TIMER
FOR J = 1 TO 20
FOR K = 1 TO 36
COUNT& = COUNT& + 1: COUNT1% = 0
GOSUB 35
IF Z(K) >= 1 THEN X(K) = Z(K): GOTO 292
IF AA(K) - UL(K) / H ^ J < LL(K) THEN GOTO 250
GOTO 260
250 L(K) = LL(K)
255 GOTO 265
260 L(K) = AA(K) - UL(K) / H ^ J
39
41
CLS
103 REM GRID% #1
G1 = X(1) + X(2) + X(3) + X(7) + X(8) + X(9)
GM1 = X(1) * X(2) * X(3) * X(7) * X(8) * X(9)
'PRINT " GM1= "; GM1
GM1 = ABS(GM1 - 720)
G1 = ABS(G1 - 21)
'PRINT: PRINT " GM1= "; GM1; " G1= "; G1
42
43
FOR GRID% = 1 TO 6
'PRINT " GRID#= "; GRID%; " GRID STRING= "; XY$(GRID%)
'INPUT " PRESS ENTER TO CONTINUE", YES
NEXT GRID%
REM ROWS
CLS
FOR ROW% = 1 TO 6: XX$(ROW%) = "": NEXT ROW%
FOR ROW% = 1 TO 6
44
FOR COL% = 1 TO 6
GOSUB 40 'CONVERT ROW% AND COL% TO K
XL$ = STR$(X(K)): XL$ = LTRIM$(XL$)
XX$(ROW%) = XX$(ROW%) + XL$
NEXT COL%
'PRINT " ROW%= "; ROW%; " ROW STRING= "; XX$(ROW%)
'INPUT " PRESS ENTER TO CONTINUE", YES
NEXT ROW%
REM COLUMNS
CLS
FOR COL% = 1 TO 6: YY$(COL%) = "": NEXT COL%
FOR COL% = 1 TO 6
FOR ROW% = 1 TO 6
GOSUB 40
XL$ = STR$(X(K)): XL$ = LTRIM$(XL$)
YY$(COL%) = YY$(COL%) + XL$
NEXT ROW%
'PRINT " COL%= "; COL%; " COLUMN STRING= "; YY$(COL%)
'INPUT " PRESS ENTER TO CONTINUE", YES
NEXT COL%
REM ROWS
CLS: CCR = 0: FOR ROW% = 1 TO 6: FOR II = 1 TO 6: TRD(ROW%, II) = 0: NEXT II:
NEXT ROW%
FOR ROW% = 1 TO 6
FOR II = 1 TO 6
TRD(ROW%, II) = TRD(ROW%, II) + CTR(ROW%, II)
'PRINT: PRINT " ROW= "; ROW%; " DIGIT= "; II; " CTR= "; CTR(ROW%, II)
NEXT II
'PRINT " ROW= "; ROW%; " TRD(ROW%, II)= "; TRD(ROW%, 1); TRD(ROW%, 2);
TRD(ROW%, 3); TRD(ROW%, 4); TRD(ROW%, 5); TRD(ROW%, 6)
'INPUT " PRESS ENTER TO CONTINUE", YES
NEXT ROW%
REM COLUMNS
CLS: CCC = 0: FOR COL% = 1 TO 6: FOR II = 1 TO 6: TCD(COL%, II) = 0: NEXT II:
NEXT COL%
FOR COL% = 1 TO 6
FOR II = 1 TO 6
46
NEXT II
TOTSUMRD = 0
FOR II = 1 TO 6
TOTSUMRD = TOTSUMRD + SUMRD(II)
NEXT II
'PRINT: PRINT " TOTSUMRD= "; TOTSUMRD
'INPUT " PRESS ENTER TO CONTINUE", YES
CLS
'PRINT: PRINT " PTT= "; PTT; " CTT= "; CTT; " GT= "; GT
'PRINT: PRINT " PMTT= "; PMTT; " CMTT= "; CMTT; " GMT= "; GMT
'PRINT: PRINT " TOTSUMGD= "; TOTSUMGD; " TOTSUMRD= "; TOTSUMRD; "
TOTSUMCD= "; TOTSUMCD
'PRINT: PRINT " T1= "; T1; " T2= "; T2; " T3= "; T3; " T4= "; T4; " T5= "; T5; " T6= ";
T6
REM CALCULATE OBJECTIVE FUNCTION
732 S = PTT + CTT + GT + PMTT + CMTT + GMT + T1 + T2 + T3 + T4 + T5 + T6 +
TOTSUMGD + TOTSUMRD + TOTSUMCD:
PRINT: PRINT " S= "; S; " MM= "; MM; " J= "; J; " I= "; I&
734 'INPUT " PRESS ENTER TO CONTINUE", YES
IF S = 0 THEN PRINT #1,: PRINT #1, " SOLUTION ACHIEVED!"; " I= "; I&; " J= "; J:
GOTO 556
IF S < MM THEN GOTO 660
GOTO 670
660 MM = S
FOR ROW% = 1 TO 6
FOR COL% = 1 TO 6
49
GOSUB 40
IF COL% <> 6 THEN GOTO 1905
PRINT #1, X(K): GOTO 1911
1905 PRINT #1, X(K);
1911 NEXT COL%
PRINT #1,
NEXT ROW%
PRINT #1,: PRINT #1, " PTT= "; PTT; " CTT= "; CTT; " GT= "; GT
PRINT #1,: PRINT #1, " PMTT= "; PMTT; " CMTT= "; CMTT; " GMT= "; GMT
PRINT #1,: PRINT #1, " TOTSUMGD= "; TOTSUMGD; " TOTSUMRD= ";
TOTSUMRD; " TOTSUMCD= "; TOTSUMCD
PRINT #1,: PRINT #1, " T1= "; T1; " T2= "; T2; " T3= "; T3; " T4= "; T4; " T5= "; T5; "
T6= "; T6
PRINT #1,: PRINT #1, " S= "; S; " MM= "; MM
PRINT #1,: PRINT #1, " J= "; J; " I= "; I&; " S= "; S; " MM= "; MM
FOR K = 1 TO 36
AA(K) = X(K)
GOSUB 35
A(ROW%, COL%) = X(K)
333 'PRINT #1, " K= "; K; " X= "; X(K)
NEXT K
670 COUNTI& = COUNTI& + 1 ': PRINT: PRINT " COUNTI= "; COUNTI&
'INPUT " PRESS ENTER TO CONTINUE", YES
50
CLS
'PRINT: PRINT " PTT= "; PTT; " CTT= "; CTT; " GT= "; GT
'PRINT: PRINT " PMTT= "; PMTT; " CMTT= "; CMTT; " GMT= "; GMT
'PRINT: PRINT " TOTSUMGD= "; TOTSUMGD; " TOTSUMRD= "; TOTSUMRD; "
TOTSUMCD= "; TOTSUMCD
'PRINT: PRINT " T1= "; T1; " T2= "; T2; " T3= "; T3; " T4= "; T4; " T5= "; T5; " T6= "; T6
COUNTI& = 0
COUNTJ& = COUNTJ& + 1: PRINT: PRINT " COUNTJ= "; COUNTJ&
'INPUT " PRESS ENTER TO CONTINUE", YES
'OP1 = 0: INPUT " ENTER '1' TO QUIT AND DUMP PRESENT SOLUTION. OP1= ", OP1
'IF OP1 = 1 THEN GOSUB 1000: GOTO 999
555 NEXT J
BEEP: BEEP
51
TF# = TIMER
PRINT: PRINT " START TIME= "; TS#; " FINISH TIME= "; TF#
ELAPSE# = (TF# - TS#): PRINT " ELAPSED TIME= "; ELAPSE#; " SECONDS"
PRINT " ELAPSED TIME= "; ELAPSE#; " SECONDS"
INPUT " PRESS ENTER TO CONTINUE", YES
GOSUB 190
GOSUB 200
INPUT " PRESS ENTER TO CONTINUE", YES
GOTO 999
FOR K = 1 TO 36
GOSUB 35
777 IF K = 18 THEN PRINT " PRESS ENTER TO CONTINUE", YES
PRINT " K= "; K; " X= "; X(K); " ROW= "; ROW%; " COL= "; COL%; " AA= "; AA(K)
NEXT K
INPUT " PRESS ENTER TO CONTINUE", YES
FOR ROW% = 1 TO 6
FOR COL% = 1 TO 6
GOSUB 40
IF COL% <> 6 THEN GOTO 905
PRINT USING "###"; X(K): GOTO 910
905 PRINT USING "###"; X(K);
910 NEXT COL%
52
PRINT
NEXT ROW%
INPUT " PRESS ENTER TO CONTINUE", YES
RETURN
ROW% = 1: COL% = K
GOTO 63
69 ROW% = K / 6 + 0.49999: ROW% = CINT(ROW%)
COL% = K - ((ROW% - 1) * 6)
63 'PRINT " K= "; K; " ROW= "; ROW%; " COL= "; COL%
'INPUT " PRESS ENTER TO CONTINUE", YES
RETURN
55
'PRINT " ROW= "; ROW%; " DIGIT= "; II; " POSITION= "; POSITION%
CTR(ROW%, II) = CTR(ROW%, II) + 1
POSITION% = INSTR(POSITION% + 1, XX$(ROW%), DIG$)
LOOP
IF CTR(ROW%, II) >= 1 THEN
'PRINT " COUNT= "; CTR(ROW%, II); " FOR DIGIT= "; II
ELSE
'PRINT " ROW= "; ROW%; " CONTAINS NO "; II
END IF
NEXT II
'INPUT " PRESS ENTER TO CONTINUE", YES
NEXT ROW%
RETURN
5000 REM SUB TO ASSIGN SEQUENTIAL VALUES TO ARRAY XXY() AND YYX()
FROM 1 TO 6 AND ZERO YYY$ ARRAY
FOR II = 1 TO 6
KK = 0
FOR JJ = 1 TO 6
KK = KK + 1
XXY(II, JJ) = KK: YYX(JJ, II) = KK: YYY$(II, JJ) = ""
'PRINT " XXY= "; XXY(II, JJ); " YYX= "; YYX(II, JJ); " II= "; II; " JJ= "; JJ
'INPUT " PRESS ENTER TO CONTINUE", YES
NEXT JJ
NEXT II
REM CHECK FOR GIVEN VALUES AND ASSIGN THEM TO 'A' ARRAY WHILE
SETTING THEIR XXY()'S AND YYX()S TO ZERO
REM THE ZERO VALUES OF XXY() AND YYX()SHOW THAT THE DIGIT HAS
ALREADY BEEN USED
FOR ROW% = 1 TO 6
FOR COL% = 1 TO 6
GOSUB 40 'CONVERT ROW, COL TO K
X(K) = 0
IF ZX(ROW%, COL%) = 0 THEN GOTO 67
58
61
34 FOR ROW% = 1 TO 6
FOR COL% = 1 TO 6
GOSUB 40
IF ZX(ROW%, COL%) <> 0 THEN GOTO 37
IF CT(ROW%, COL%) = 1 AND ZX(ROW%, COL%) = 0 THEN GOTO 38 'FOUND
NEW PERMANENT VALUE
GOTO 37
62
999 END
63
Bibliography
Banbura, M. A. (2010, May). Maximum Likelihood Estimation of Factor Models on Data Sets
with Arbitrary Pattern of Missing Data. Social Science Research Network, Retrieved
Sept. 1, 2012 from SSRN Electronic Library (Working Paper Series No. 1189/May
2010:http://ssrn.com/abstract_id=1598302. Retrieved from
http://ssrn.com/abstract_id=1598302.
Isaacs, R. (1953). Optimal Horse Race Bets. American Math Monthly, 60, No. 5, 310-315.
Lashover, J. H. (2012, November 12). Monte Carlo Marching. Academia.edu and
ResearchGate.net.
Lashover, J. H. (2014, August 28). A Chemical Engineer Goes to the Horse Races. Retrieved
from www.academia.edu and www.ResearchGate.com.
64