Nп/п : 31 из 100
 От   : Ahmed MELAHI                        2:5075/128        27 сен 23 05:19:23
 К    : none) (albert                                         27 сен 23 15:23:07
 Тема : Re: new version of magic hexagon program
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<45ecaf3c-8eca-4347-90c9-f53c093cb448n@googlegroups.com> 2e7f98a5
@REPLY:
3a2d6287
@REPLYADDR Ahmed MELAHI
<ahmed.melahi@univ-bejaia.dz>
@REPLYTO 2:5075/128 Ahmed MELAHI
@CHRS: CP866 2
@RFC: 1 0
@RFC-References:
<3b96660a-56e6-4a84-ac82-2758fe6b9d06n@googlegroups.com>
@RFC-Message-ID:
<45ecaf3c-8eca-4347-90c9-f53c093cb448n@googlegroups.com>
@TZUTC: -0700
@PID: G2/1.0
@TID: FIDOGATE-5.12-ge4e8b94
Le mercredi 27 septembre 2023 ? 13:46:27 UTC+2, none albert a ?crit :
> In article <3b96660a-56e6-4a84...@googlegroups.com>,
> Ahmed MELAHI <ahmed....@univ-bejaia.dz> wrote: 
> >Hi, 
> >I rewrote the program for the magic hexagon. 
 > >It appears elegant without any loss of performance. I think it
is faster than the last versions I have already posted. 
> > 
> >Here begin the program: 
> > 
> >\\ 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. 
> > 
> >: values 0 ?do 0 value loop ; 
> >19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS 
> > 
> >create marking table 77 allot 
> >marking table 77 1 fill 
> > 
> >marking table 38 + value marked 
> >marked 20 erase 
> > 
> >: -- 2 .r 2 spaces ; 
> >: .mag hex 
> > 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 
> >; 
> > 
> >0 value nloops prec 
> >0 value nloops 
> >0 value constraint num 
> >20 value max num constraints 
> >create loop loc max num constraints allot 
> >loop loc max num constraints erase 
> >: mark 1 swap marked + c! ; 
> >: unmark 0 swap marked + c! ; 
> >: marked? marked + c@ 0= ; 
> > 
> >: .-- nloops 1+ to nloops postpone do postpone i ; immediate 
> >: ?, postpone dup postpone marked? postpone if postpone mark ; immediate 
 > >: --> postpone to constraint num 1+ to constraint num nloops
nloops prec <> if 1 loop loc constraint num + c! nloops to nloops prec
then ; immediate 
> >: constraints begin( marked 20 erase ; 
> >: finish: nloops 0 do postpone unloop loop postpone exit ; immediate 
> >: --- ; immediate 
> >:  begin  marked 20 erase ; 
 > >: | postpone unmark postpone else postpone drop postpone then
loop loc constraint num + c@ if postpone loop then constraint num 1- to
constraint num ; immediate 
> >:  end  ; immediate 
> > 
> >: solve 
> >  begin  
> > 20 1 .-- --> vA vA ?, 
> > 20 1 .-- --> vB vB ?, 
> > 38 vA vB + - --- --> vC vC ?, 
> > 20 1 .-- --> vG vG ?, 
> > 38 vC vG + - --- --> vL vL ?, 
> > 20 1 .-- --> vP vP ?, 
> > 38 vL vP + - --- --> vS vS ?, 
> > 20 1 .-- --> vR vR ?, 
> > 38 vS vR + - --- --> vQ vQ ?, 
> > 20 1 .-- --> vM vM ?, 
> > 38 vQ vM + - --- --> vH vH ?, 
> > 38 vA vH + - --- --> vD vD ?, 
> > 20 1 .-- --> vE vE ?, 
> > 38 vD vE + vG + - --- --> vF vF ?, 
> > 38 vB vF + vP + - --- --> vK vK ?, 
> > 38 vG vK + vR + - --- --> vO vO ?, 
> > 38 vP vO + vM + - --- --> vN vN ?, 
> > 38 vR vN + vD + - --- --> vI vI ?, 
> > 38 vH vI + vK + vL + - --- --> vJ vJ ?, 
> > 
 > > finish: vJ | vI | vN | vO | vK | vF | vE | vD | vH |
vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA | 
> >  end  
> >; 
> > 
> > 
> >Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC: 
> >gforth: 4.5 ms 
> >vfxforth: 0.734 ms 
> >iforth: 0.976 ms 
> >
> I have investigated the lisp versions. They run in the seconds (10-15) 
> where the Forth program runs in 33 ms (ciforth) 16 ms (gforth) and faster 
> still at home. 
> Both programs rely on macro expansion. 

> He las the comparison is not fair, because lisp calculates all solutions 
> (which doesn`t make sense because all solutions are equivalent, but anyway.) 

> Could some one alter the program, such that all solutions are generated? 
> Then we can go boasting on comp.lang.lisp. 

> >Enjoy
> > 

> Groetjes Albert 
> -- 
> Don`t praise the day before the evening. One swallow doesn`t make spring. 
> You must not say "hey" before you have crossed the bridge. Don`t sell the 
> hide of the bear until you shot it. Better one bird in the hand than ten in 
> the air. First gain is a cat spinning. - the Wise from Antrim -
Hi,
To get the 12 solutions with printing them on the terminal:
 In the word solve: put the word finish: in parenthesis ( comment
it out), and before it add the word .mag hex
with gforth: I got 265200 us ( printing included)
For timing, I use : 
utime solve utime d>f d>f f- f. ." us"

Enjoy

--- G2/1.0
 * Origin: usenet.network (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5058/104 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх