In how many distinct ways can I fill a 3×3 grid with the integers 1 through 9 (each occurring exactly once) such that all cells in the grid are coprime with the neighbors? (Note that rotations and reflections count as distinct ways of filling the grid)
Note: A cell's neighbors are the cells that share a full side with it. If two cells share a corner but not a side, they are not neighbors.
As indicated by the total at bottom of the output, there are 2016 total solutions. This includes rotations and reflections, so only 252 are shown below. The only rotation/reflection listed is the one in which the lowest corner is at the upper left and the top right corner is lower than the bottom left.
123
654
789
123
658
749
123
674
589
123
678
549
132
549
678
132
589
674
132
749
658
132
789
654
132
945
876
132
947
856
132
985
476
132
987
456
134
529
678
134
589
672
134
729
658
134
789
652
134
925
876
134
927
856
143
652
789
143
658
729
143
672
589
143
678
529
156
327
894
156
347
892
156
927
834
156
947
832
165
274
983
165
278
943
165
472
983
165
478
923
165
872
943
165
874
923
167
254
983
167
258
943
167
452
983
167
458
923
167
852
943
167
854
923
176
325
894
176
345
892
176
925
834
176
945
832
183
652
749
183
654
729
183
672
549
183
674
529
192
345
876
192
347
856
192
385
476
192
387
456
192
543
678
192
583
674
192
743
658
192
783
654
194
325
876
194
327
856
194
523
678
194
583
672
194
723
658
194
783
652
213
945
876
213
947
856
213
985
476
213
987
456
216
345
798
216
345
879
216
345
897
216
347
859
216
347
895
216
357
894
216
375
894
216
385
794
216
945
738
216
945
837
216
945
873
216
947
835
216
947
853
216
957
834
216
975
834
216
985
734
234
159
678
234
179
658
234
185
679
234
185
976
234
187
659
234
187
956
234
189
657
234
189
675
234
519
678
234
579
618
234
581
679
234
581
976
234
587
619
234
587
916
234
589
617
234
719
658
234
759
618
234
781
659
234
781
956
234
785
619
234
785
916
234
789
615
234
915
876
234
917
856
234
951
876
234
957
816
234
971
856
234
975
816
234
981
576
234
981
756
234
985
716
234
987
516
235
149
678
235
189
674
235
749
618
235
789
614
235
941
876
235
947
816
237
941
856
237
945
816
238
145
976
238
147
956
238
541
976
238
547
916
238
741
956
238
745
916
253
941
876
253
947
816
253
981
476
253
987
416
256
317
894
256
341
798
256
341
879
256
341
897
256
347
819
256
371
894
256
381
794
256
917
834
256
941
738
256
941
837
256
941
873
256
947
813
256
971
834
256
981
734
273
941
856
273
945
816
273
981
456
273
985
416
276
315
894
276
341
859
276
341
895
276
345
819
276
351
894
276
915
834
276
941
835
276
941
853
276
945
813
276
951
834
294
153
678
294
173
658
294
183
657
294
183
675
294
185
673
294
187
653
294
315
876
294
317
856
294
351
876
294
357
816
294
371
856
294
375
816
294
381
576
294
381
756
294
385
716
294
387
516
294
513
678
294
573
618
294
581
673
294
583
617
294
587
613
294
713
658
294
753
618
294
781
653
294
783
615
294
785
613
295
143
678
295
183
674
295
341
876
295
347
816
295
743
618
295
783
614
297
341
856
297
345
816
314
529
678
314
729
658
316
527
894
316
725
894
325
416
987
325
816
947
327
416
985
327
816
945
345
216
987
345
816
927
347
216
985
347
816
925
354
129
678
354
729
618
356
127
894
356
721
894
374
129
658
374
529
618
376
125
894
376
521
894
385
216
947
385
416
927
387
216
945
387
416
925
416
325
798
416
325
879
416
325
897
416
327
859
416
327
895
416
925
738
416
925
837
416
927
835
435
129
678
435
729
618
435
921
876
435
927
816
437
921
856
437
925
816
438
125
976
438
127
956
438
521
976
438
527
916
438
721
956
438
725
916
456
321
798
456
321
879
456
321
897
456
327
819
456
921
738
456
921
837
476
321
859
476
321
895
476
325
819
476
921
835
495
123
678
495
321
876
495
327
816
495
723
618
497
321
856
497
325
816
2016 done
DefDbl A-Z
Dim crlf$, sq(4, 4)
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
s$ = "123456789": h$ = s
Do
p = 0
For r = 1 To 3
For c = 1 To 3
p = p + 1
sq(r, c) = Val(Mid(s, p, 1))
Next
Next
good = 1
DoEvents
For r = 1 To 3
For c = 1 To 3
For dr = 0 To 1
For dc = 0 To 1
If (dr <> 0 Or dc <> 0) And (dr = 0 Or dc = 0) Then
r1 = r + dr: c1 = c + dc
If sq(r1, c1) <> 0 Then
If gcd(sq(r1, c1), sq(r, c)) > 1 Then good = 0: Exit For
End If
End If
Next
DoEvents
If good = 0 Then Exit For
Next
If good = 0 Then Exit For
Next
If good = 0 Then Exit For
Next
If good Then
If Mid(s, 1, 1) < Mid(s, 3, 1) And Mid(s, 1, 1) < Mid(s, 7, 1) And Mid(s, 1, 1) < Mid(s, 9, 1) Then
If Mid(s, 3, 1) < Mid(s, 7, s) Then
Text1.Text = Text1.Text & Left(s, 3) & crlf & Mid(s, 4, 3) & crlf & Right(s, 3) & crlf & crlf
End If
End If
ct = ct + 1
End If
permute s
Loop Until s = h
Text1.Text = Text1.Text & ct & " done"
DoEvents
End Sub
Function gcd(a, b)
x = a: y = b
Do
q = Int(x / y)
z = x - q * y
x = y: y = z
Loop Until z = 0
gcd = x
End Function
|
Posted by Charlie
on 2019-10-17 13:53:36 |