Some strange result
Sylvain Julmy <[email protected]>
| Newsgroups | gmane.comp.gnu.prolog.general |
|---|---|
| Message-ID | <[email protected]> |
Hi, at first, excuse me for my English, I'm not very good at... At the School of Engineering and Architecture of Fribourg, Switzerland, we have a Prolog course in the thirs year of the Bachelor cursus. In an exercice, we need to study a programm wo resolve the Hanoi tower problem. I have create a simple predicat that's simply execute the solve1 predicat (see attachment : hanoi.pl) and the time computation is increasing and we dont know why. That's the output when i run my test_solve1 predicat : | ?- test_solve1(25,10). 25: nbrMoves = 1023.0 6ms 24: nbrMoves = 1023.0 9ms 23: nbrMoves = 1023.0 15ms 22: nbrMoves = 1023.0 18ms 21: nbrMoves = 1023.0 22ms 20: nbrMoves = 1023.0 23ms 19: nbrMoves = 1023.0 25ms 18: nbrMoves = 1023.0 25ms 17: nbrMoves = 1023.0 26ms 16: nbrMoves = 1023.0 27ms 15: nbrMoves = 1023.0 31ms 14: nbrMoves = 1023.0 34ms 13: nbrMoves = 1023.0 168ms 12: nbrMoves = 1023.0 204ms 11: nbrMoves = 1023.0 223ms 10: nbrMoves = 1023.0 251ms 9: nbrMoves = 1023.0 270ms 8: nbrMoves = 1023.0 291ms 7: nbrMoves = 1023.0 313ms 6: nbrMoves = 1023.0 334ms 5: nbrMoves = 1023.0 357ms 4: nbrMoves = 1023.0 381ms 3: nbrMoves = 1023.0 399ms 2: nbrMoves = 1023.0 419ms 1: nbrMoves = 1023.0 441ms I hope someone could find a good response to this ! Dear, Sylvain Julmy School of Engineering and Architecture of Fribourg Switzerland _______________________________________________ Users-prolog mailing list [email protected] https://lists.gnu.org/mailman/listinfo/users-prolog
hanoi.pl
(application/x-perl, 937 B)
test_solve1(0,_) :- !.
test_solve1(N,H) :-
time(N,Moves,solve1(H,Moves)),
nl,
N1 is N - 1,
test_solve1(N1,H).
:- dynamic(hanoiDyn/5).
% --- hanoiDyn1(+NbOfDiscs, ?A, ?B, ?C, ?NbOfMoves).
hanoiDyn(1,_A,_B,_,1.0). %--- move _A to _B
hanoiDyn(N,A,B,C,Moves) :-
N>1,
N1 is N-1,
hanoiDyn(N1,A,C,B,Moves1), %--- move N-1 discs
asserta((hanoiDyn(N1,A,C,B,Moves1) :-! )),
hanoiDyn(N1,C,B,A,Moves2), %--- move N-1 discs
retract((hanoiDyn(N1,A,C,B,_ ) :-! )),
Moves is Moves1 + Moves2 + 1.
solve1(N, Moves) :-
[A, B, C] = [a, b, c], % L2
hanoiDyn(N, A, B, C, Moves). % L1
time(Text1,Text2,Goal) :-
cpu_time(CPU),
Goal,
cpu_time(CPU2),
CPUT is CPU2-CPU,
tab(1), write(Text1), write(': ') ,write('nbrMoves = '),
write(Text2),write(' '), write(CPUT), write('ms').