RosettaCodeData/Task/Josephus-problem/Phix/josephus-problem.phix

21 lines
581 B
Plaintext

function Josephus(sequence prisoners, integer step, survivors)
integer n = length(prisoners), nn = n
integer p = 0
while n>survivors do
integer found = 0
while found!=step do
p = iff(p=nn?1:p+1)
found += prisoners[p]!=-1
end while
-- (if you want a kill list, build it here!)
prisoners[p] = -1
n -= 1
end while
return remove_all(-1,prisoners)
end function
?Josephus(tagset(5),2,1)
?Josephus(tagset(41),3,1)
?Josephus(tagset(41),3,3)
?Josephus({"Joe","Jack","William","John","James"},2,1)