-->
function contractacycle(integer n, integer k, s)
sequence living = tagset(n)
integer startPosition = k, i, lasti
while n!=s do -- Keep going round the circle until only s prisoners remain.
integer circleSize = n
if (n < k) then
i = mod(startPosition-1,circleSize) + 1
living = living[1..i-1]&living[i+1..$]
n -= 1
lasti = i
else
for i=startPosition to circleSize by k do
living[i] = -1
n -= 1
if (n = s) then exit end if -- Not Groovy, see note
lasti = i
end for
living = remove_all(-1,living)
end if
startPosition = lasti + k - circleSize
end while
return living
end function