-
Notifications
You must be signed in to change notification settings - Fork 0
/
melahi2.4th
731 lines (597 loc) · 20.3 KB
/
melahi2.4th
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
\ Newsgroups: comp.lang.forth
\ Date: Tue, 21 Feb 2023 10:01:16 -0800 (PST)
\ Message-ID: <[email protected]>
\ Subject: Re: Magic Hexagon
\ From: Ahmed MELAHI <[email protected]>
\ ...
\ HI, Thanks for testing.
\ The previous program was done in one shot, so it is not optimized, and contains some problems that concerns marking and unmarking already pruned values.
\ Hereafter, I modified the program so that :
\ - problem of marking and umarking is fixed
\ - the program now can get the 12 solutions
\ - we can get just one solution
\ tested with gforth:
\ - just one solution : 0.07745 second
\ - 12 solutions : 0.243362 seconds
\ tested with gforth-fast:
\ - just one solution: 0.074116 second
\ - 12 solutions: 0.129788 seconds
\ I did not optimize it for the moment, it still be one long long long word.
\ In fact, the approach is:
\ - fill table ABC (using 1st constraint)
\ - cycle through entries of the table ABC: for each entry of ABC do:
\ - fill table GL
\ - cycle through entries of GL: for each entry of GL do:
\ - fill table PS
\ - cycle through PS: for each entry of PS do:
\ - .....
\ -......
\ - caculate I
\ - for this value of I do:
\ -calulate J
\ - if J is ok, put this solution in a the table "solutions" and increase the count of solutions
\ this is the structure: fill table, cycle through this table (fill table, cycle....
\ the search is systematic: (Dynamic Programing??!!!).
\ ---------------------here begin the listing:
\ Place the integers 1..19 in the following Magic Hexagon of rank 3
\ __A_B_C__
\ _D_E_F_G_
\ H_I_J_K_L
\ _M_N_O_P_
\ __Q_R_S__
\ so that the sum of all numbers in a straight line (horizontal and diagonal)
\ is equal to 38.
\ here begins the application
0 value vA
0 value vB
0 value vC
0 value vD
0 value vE
0 value vF
0 value vG
0 value vH
0 value vI
0 value vJ
0 value vK
0 value vL
0 value vM
0 value vN
0 value vO
0 value vP
0 value vQ
0 value vR
0 value vS
0 value nth_ABC
0 value nth_GL
0 value nth_PS
0 value nth_RQ
0 value nth_MH
0 value nth_EF
0 value vD_ok
0 value vK_ok
0 value vO_ok
0 value vN_ok
0 value vI_ok
0 value vJ_ok
0 value n_sol
0 value solution_found_?
create marked 20 allot
marked 20 erase
create solutions 10 20 * allot
create ABC 19 18 * 3 * allot
create GL 16 2 * allot
create PS 14 2 * allot
create RQ 12 2 * allot
create MH 10 2 * allot
create EF 8 2 * allot
: solve
marked 20 erase
0 to n_sol
0 to nth_ABC
0 to nth_GL
0 to nth_PS
0 to nth_RQ
0 to nth_MH
0 to nth_EF
\ ABC fill ABC
20 1
do
i to vA
1 vA marked + c!
20 1
do
i to vB
vB marked + c@ 0=
if
1 vB marked + c!
38 vA vB + - to vC
vC marked + c@ 0=
vC 0> and
vC 20 < and
if
vA 0 nth_ABC 3 * + ABC + c!
vB 1 nth_ABC 3 * + ABC + c!
vC 2 nth_ABC 3 * + ABC + c!
nth_ABC 1+ to nth_ABC
then
then
0 vB marked + c!
loop
0 vA marked + c!
loop
\ cycle through ABC
nth_ABC 0
do
marked 20 erase
0 to nth_GL
\ cr ." ABC: " i .
0 i 3 * + ABC + c@ to vA
1 i 3 * + ABC + c@ to vB
2 i 3 * + ABC + c@ to vC
\ cr vA . vB . vC . .s \ -----------------------------------
\ GL
20 1
do
i to vG
vG marked + c@ 0=
if
1 vG marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
38 vC vG + - to vL
vL marked + c@ 0=
vL 0> and
vL 20 < and
if
vG 0 nth_GL 2 * + GL + c!
vL 1 nth_GL 2 * + GL + c!
nth_GL 1+ to nth_GL
then
then
0 vG marked + c!
loop
\ cycle through GL
nth_GL 0
?do
marked 20 erase
0 to nth_PS
\ cr ." GL:" i .
0 i 2 * + GL + c@ to vG
1 i 2 * + GL + c@ to vL
\ cr vA . vB . vC . vG . vL . .s \ ----------------------------------------
\ PS
20 1
do
i to vP
vP marked + c@ 0=
if
1 vP marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
38 vL vP + - to vS
vS marked + c@ 0=
vS 0> and
vS 20 < and
if
vP 0 nth_PS 2 * + PS + c!
vS 1 nth_PS 2 * + PS + c!
nth_PS 1+ to nth_PS
then
then
0 vP marked + c!
loop
\ cycle through PS
nth_PS 0
?do
marked 20 erase
0 to nth_RQ
\ ." PS: " i .
0 i 2 * + PS + c@ to vP
1 i 2 * + PS + c@ to vS
\ cr vA . vB . vC . vG . vL . vP . vS . .s \ -------------------
\ RQ
20 1
do
i to vR
vR marked + c@ 0=
if
1 vR marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
38 vS vR + - to vQ
vQ marked + c@ 0=
vQ 0> and
vQ 20 < and
if
vR 0 nth_RQ 2 * + RQ + c!
vQ 1 nth_RQ 2 * + RQ + c!
nth_RQ 1+ to nth_RQ
then
then
0 vR marked + c!
loop
\ cycle through RQ
nth_RQ 0
?do
marked 20 erase
0 to nth_MH
0 i 2 * + RQ + c@ to vR
1 i 2 * + RQ + c@ to vQ
\ cr vA . vB . vC . vG . vL . vP . vS . vR . vQ . .s \ ------------------------------
\ MH
20 1
do
i to vM
vM marked + c@ 0=
if
1 vM marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
38 vQ vM + - to vH
vH marked + c@ 0=
vH 0> and
vH 20 < and
if
vM 0 nth_MH 2 * + MH + c!
vH 1 nth_MH 2 * + MH + c!
nth_MH 1+ to nth_MH
then
then
0 vM marked + c!
loop
\ cycle through MH
nth_MH 0
?do
marked 20 erase
0 i 2 * + MH + c@ to vM
1 i 2 * + MH + c@ to vH
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
\ cr vA . vB . vC . vG . vL . vP . vS . vR . vQ . vM . vH . .s \ -------------------------------------------------
\ calculate D (38-A-H = D)
0 to vD_ok
38 vA vH + - to vD
vD marked + c@ 0=
vD 0> and
vD 20 < and
if
1 to vD_ok
then
0 vD marked + c!
0 to nth_EF
vD_ok
if
\ EF
\ cr vA . vB . vC . vG . vL . vP . vS . vR . vQ . vM . vH . vD . .s \ -------------------------------------------------
20 1
do
i to vE
vE marked + c@ 0=
if
marked 20 erase
1 vE marked + c!
1 vD marked + c!
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
38 vD vE + vG + - to vF
vF marked + c@ 0=
vF 0> and
vF 20 < and
if
vE 0 nth_EF 2 * + EF + c!
vF 1 nth_EF 2 * + EF + c!
nth_EF 1+ to nth_EF
then
then
0 vE marked + c!
loop \ EF
nth_EF 0
?do
marked 20 erase
0 i 2 * + EF + c@ to vE
1 i 2 * + EF + c@ to vF
1 vE marked + c!
1 vF marked + c!
1 vD marked + c!
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
\ cr vA . vB . vC . vG . vL . vP . vS . vR . vQ . vM . vH . vD . vE . vF . .s \ -------------------------------------------------
\ calculate K (K = 38-B-F-P)
0 to vK_ok
38 vB vF + vP + - to vK
vK marked + c@ 0=
vK 0> and
vK 20 < and
if
1 to vK_ok
then
0 vK marked + c!
vK_ok
if
\ calculate O (O = 38-G-K-R)
1 vK marked + c!
1 vE marked + c!
1 vF marked + c!
1 vD marked + c!
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
\ cr vA . vB . vC . vG . vL . vP . vS . vR . vQ . vM . vH . vD . vE . vF . vK . .s \ -------------------------------------------------
0 to vO_ok
38 vG vK + vR + - to vO
vO marked + c@ 0=
vO 0> and
vO 20 < and
if
1 to vO_ok
then
0 vO marked + c!
vO_ok
if
\ calculate N (N = 38-P-O-M)
1 vO marked + c!
1 vK marked + c!
1 vE marked + c!
1 vF marked + c!
1 vD marked + c!
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
0 to vN_ok
38 vP vO + vM + - to vN
vN marked + c@ 0=
vN 0> and
vN 20 < and
if
1 to vN_ok
then
0 vN marked + c!
vN_ok
if
\ calculate I (I = 38-R-N-D)
1 vN marked + c!
1 vO marked + c!
1 vK marked + c!
1 vE marked + c!
1 vF marked + c!
1 vD marked + c!
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
0 to vI_ok
38 vR vN + vD + - to vI
vI marked + c@ 0=
vI 0> and
vI 20 < and
if
1 to vI_ok
then
0 vI marked + c!
vI_ok
if
\ calculate J (J = 38-H-I-K-L)
1 vI marked + c!
1 vN marked + c!
1 vO marked + c!
1 vK marked + c!
1 vE marked + c!
1 vF marked + c!
1 vD marked + c!
1 vM marked + c!
1 vH marked + c!
1 vR marked + c!
1 vQ marked + c!
1 vP marked + c!
1 vS marked + c!
1 vG marked + c!
1 vL marked + c!
1 vA marked + c!
1 vB marked + c!
1 vC marked + c!
0 to vJ_ok
38 vH vI + vK + vL + - to vJ
vJ marked + c@ 0=
vJ 0> and
vJ 20 < and
if
1 to vJ_ok
1 to solution_found_?
then
0 vJ marked + c!
vJ_ok
if
1 vJ marked + c!
n_sol 1+ to n_sol
vA 0 n_sol 20 * + solutions + c!
vB 1 n_sol 20 * + solutions + c!
vC 2 n_sol 20 * + solutions + c!
vD 3 n_sol 20 * + solutions + c!
vE 4 n_sol 20 * + solutions + c!
vF 5 n_sol 20 * + solutions + c!
vG 6 n_sol 20 * + solutions + c!
vH 7 n_sol 20 * + solutions + c!
vI 8 n_sol 20 * + solutions + c!
vJ 9 n_sol 20 * + solutions + c!
vK 10 n_sol 20 * + solutions + c!
vL 11 n_sol 20 * + solutions + c!
vM 12 n_sol 20 * + solutions + c!
vN 13 n_sol 20 * + solutions + c!
vO 14 n_sol 20 * + solutions + c!
vP 15 n_sol 20 * + solutions + c!
vQ 16 n_sol 20 * + solutions + c!
vR 17 n_sol 20 * + solutions + c!
vS 18 n_sol 20 * + solutions + c!
\ +---------------------------------------------------------------------------------------------------------------+
\ | to get just one solution uncomment the line hereafter, to get all solutions (12) comment the line hereafter. |
\ +---------------------------------------------------------------------------------------------------------------+
cr ." one solution found." unloop unloop unloop unloop unloop unloop exit
then 0 vJ marked + c! \ vJ_ok
then 0 vI marked + c! \ vI_ok
then 0 vN marked + c! \ vN_ok
then 0 vO marked + c! \ vO_ok
then 0 vK marked + c! \ vK_ok
loop 0 vE marked + c! 0 vF marked + c! \ EF
then 0 vD marked + c! \ vD_ok
loop 0 vM marked + c! 0 vH marked + c! \ MH
loop 0 vR marked + c! 0 vQ marked + c! \ RQ
loop 0 vP marked + c! 0 vS marked + c! \ PS
loop 0 vG marked + c! 0 vL marked + c! \ GL
loop \ 0 vA marked + c! 0 vB marked + c! 0 vC marked + c! \ ABC
;
: .solution
cr n_sol . ." solutions found."
n_sol 1+ 1
?do
0 i 20 * + solutions + c@ to vA
1 i 20 * + solutions + c@ to vB
2 i 20 * + solutions + c@ to vC
3 i 20 * + solutions + c@ to vD
4 i 20 * + solutions + c@ to vE
5 i 20 * + solutions + c@ to vF
6 i 20 * + solutions + c@ to vG
7 i 20 * + solutions + c@ to vH
8 i 20 * + solutions + c@ to vI
9 i 20 * + solutions + c@ to vJ
10 i 20 * + solutions + c@ to vK
11 i 20 * + solutions + c@ to vL
12 i 20 * + solutions + c@ to vM
13 i 20 * + solutions + c@ to vN
14 i 20 * + solutions + c@ to vO
15 i 20 * + solutions + c@ to vP
16 i 20 * + solutions + c@ to vQ
17 i 20 * + solutions + c@ to vR
18 i 20 * + solutions + c@ to vS
cr
." A=" vA 2 .r space
." B=" vB 2 .r space
." C=" vC 2 .r space
." D=" vD 2 .r space
." E=" vE 2 .r space
." F=" vF 2 .r space
." G=" vG 2 .r space
." H=" vH 2 .r space
." I=" vI 2 .r space
." J=" vJ 2 .r space
." K=" vK 2 .r space
." L=" vL 2 .r space
." M=" vM 2 .r space
." N=" vN 2 .r space
." O=" vO 2 .r space
." P=" vP 2 .r space
." Q=" vQ 2 .r space
." R=" vR 2 .r space
." S=" vS 2 .r
loop
;
: -- 2 .r 2 spaces ;
: .mag_hex
cr n_sol . ." solutions found."
n_sol 1+ 1
?do
0 i 20 * + solutions + c@ to vA
1 i 20 * + solutions + c@ to vB
2 i 20 * + solutions + c@ to vC
3 i 20 * + solutions + c@ to vD
4 i 20 * + solutions + c@ to vE
5 i 20 * + solutions + c@ to vF
6 i 20 * + solutions + c@ to vG
7 i 20 * + solutions + c@ to vH
8 i 20 * + solutions + c@ to vI
9 i 20 * + solutions + c@ to vJ
10 i 20 * + solutions + c@ to vK
11 i 20 * + solutions + c@ to vL
12 i 20 * + solutions + c@ to vM
13 i 20 * + solutions + c@ to vN
14 i 20 * + solutions + c@ to vO
15 i 20 * + solutions + c@ to vP
16 i 20 * + solutions + c@ to vQ
17 i 20 * + solutions + c@ to vR
18 i 20 * + solutions + c@ to vS
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
loop
;
utime solve utime d>f d>f f- cr cr ." execution time : " f. ." micro seconds." cr cr .solution cr cr .mag_hex
: timing_10
utime
10 0
do
solve
loop
utime
d>f d>f f- 10e f/
cr cr ." Mean execution time : " f. ." micro seconds."
;