I have no idea what I’m doing
local n = 41
function josephus(n)
local positions = {}
local tokill = 2
local nextkiller = 1
for i = 1, n do
positions[i] = i
end
while #positions>1 do
tokill = next(positions, nextkiller)
print(tokill)
nextkiller = next(positions, tokill)
table.remove(positions, tokill)
wait()
end
end
josephus(n)
If anyone isn’t familiar with this, I’m basically trying to remove every other item in a table over and over until I’m only left with one (Like the below image), at least thats the intended effect.
In the script where n is 41 (40 numbers I have to remove in that fashion), the execution order should be 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 1, 5, 9, 13, etc
, but obviously it’s not turning out that way.
So can someone tell me what I did wrong? It does it right until it has to remove number 30, which from that point it does all of it wrong, Maybe I’m not using next right? Thanks in advance.