(phixonline)-->
--demo/rosetta/Josephus.exw
constant show_all = true,
show_slow = false,
show_skipping = false,
show_linkedlist = false,
show_sliding_queue = false,
show_contractacycle = false,
show_contractalot = false,
show_recursive = false,
show_iterative = false,
show_iterative2 = true
constant TAGSET = #01,
ITER = #02,
ITER2 = #04,
SLOW = #08,
ONES = #10
constant tests = {{41,3,1,false},
{41,3,3,false},
{5,2,1,false},
{5,4,1,false},
{50,2,1,false},
{60,3,1,false},
{23482,3343,3,true},
{23482,3343,1,true},
{41,3,6,false}}
procedure test(string name, integer flags)
atom t0 = time()
integer rid = routine_id(name)
for i=1 to length(tests) do
integer {prisoners, step, survivors, slow} = tests[i]
if (not and_bits(flags,ONES) or survivors=1)
and (not slow or show_slow or not and_bits(flags,SLOW)) then
sequence res
if and_bits(flags,ONES) then
-- (recursive does not take a 3rd param)
res = {rid(prisoners,step)}
elsif and_bits(flags,TAGSET) then
res = rid(tagset(prisoners),step,survivors)
elsif and_bits(flags,ITER) then
res = {}
for s=0 to survivors-1 do
res &= rid(prisoners,step,s)
end for
elsif and_bits(flags,ITER2) then
res = {}
for s=prisoners-survivors+1 to prisoners do
res &= rid(prisoners,step,s)
end for
else
res = rid(prisoners,step,survivors)
end if
printf(1,"%s(%d,%d,%d) = %v\n",{name,prisoners,step,survivors,res})
end if
end for
?elapsed(time()-t0)
end procedure
if show_all or show_skipping then test("skipping",TAGSET+SLOW) end if
if show_all or show_linkedlist then test("linked_list",TAGSET+SLOW) end if
if show_all or show_sliding_queue then test("sliding_queue",TAGSET+SLOW) end if
if show_all or show_contractacycle then test("contractacycle",SLOW) end if
if show_all or show_contractalot then test("contractalot",NULL) end if
if show_all or show_recursive then test("recursive",ONES) end if
if show_all or show_iterative then test("iterative",ITER) end if
if show_all or show_iterative2 then test("iterative2",ITER2) end if