Towers of Hanoi: Difference between revisions

→‎{{header|Picat}}: Fixed tags, removed comment about table/0
(→‎{{header|Picat}}: Fixed tags, removed comment about table/0)
Line 4,435:
 
=={{header|Picat}}==
This uses tabling, of course.
This uses tabling, of course. An issue is that if <code>table/0</code> is placed before the base case (the 0 case), it's very fast and the counting is correct, but the printing is not correct. However, printing works if <code>table/0</code> is placed before the recursion step, but it's quite slower. See below for an experiment/showcase on this.
 
<lang Picat>main =>
hanoi(3,true),
nl,
hanoi(24,false),
endnl,
hanoi(64,false),
nl.
Line 4,450 ⟶ 4,451:
nl.
% See comment above on the placing of table/0
% table
table
move(0, _From, _To, _Via, _Print) = 0 => true.
Line 4,459 ⟶ 4,458:
printf("Move disk %w from pole %w to pole %w\n", N, From, To)
end,
Count2 = move(N - 1, Via, To, From, Print),
Count = Count1+Count2+1.</lang>
 
{{out}}
Line 4,474 ⟶ 4,473:
 
N=24
count=16777215, theoretical=16777215</pre>
 
===Faster counting===
Placing <code>table/0</code> before the base case is fast but - as mentioned above - the step printing is not correct. It only prints 189 steps, all the rest steps are skipped.
<lang Picat>table
move(0, _From, _To, _Via, _Print) = 0 => true.
move(N, From, To, Via, Print) = Count =>
Count1 = move(N - 1, From, Via, To, Print),
if Print then
printf("Move disk %w from pole %w to pole %w\n", N, From, To)
end,
Count2 = move(N - 1, Via, To, From, Print),
Count = Count1+Count2+1.
</lang>
 
{{out}}
<pre>N=64
 
count=18446744073709551615, theoretical=18446744073709551615
 
<pre>N=64
CPU time 0.0 seconds.</pre>
count=18446744073709551615, theoretical=18446744073709551615</pre>
 
=={{header|PicoLisp}}==
495

edits