Josephus problem: Difference between revisions
Content added Content deleted
m (J: actually that was negation, not subtraction - so let's go a bit further in the classic notation direction) |
|||
Line 1,979: | Line 1,979: | ||
remaining: 30 |
remaining: 30 |
||
remaining 4: 3,34,15,30 |
remaining 4: 3,34,15,30 |
||
</pre> |
|||
=={{header|VBScript}}== |
|||
<lang vb> |
|||
Function josephus(n,k,s) |
|||
Set prisoner = CreateObject("System.Collections.ArrayList") |
|||
For i = 0 To n - 1 |
|||
prisoner.Add(i) |
|||
Next |
|||
index = -1 |
|||
Do Until prisoner.Count = s |
|||
step_count = 0 |
|||
Do Until step_count = k |
|||
If index+1 <= prisoner.Count-1 Then |
|||
index = index+1 |
|||
Else |
|||
index = (index+1)-(prisoner.Count) |
|||
End If |
|||
step_count = step_count+1 |
|||
Loop |
|||
prisoner.RemoveAt(index) |
|||
index = index-1 |
|||
Loop |
|||
For j = 0 To prisoner.Count-1 |
|||
If j < prisoner.Count-1 Then |
|||
josephus = josephus & prisoner(j) & "," |
|||
Else |
|||
josephus = josephus & prisoner(j) |
|||
End If |
|||
Next |
|||
End Function |
|||
'testing the function |
|||
WScript.StdOut.WriteLine josephus(5,2,1) |
|||
WScript.StdOut.WriteLine josephus(41,3,1) |
|||
WScript.StdOut.WriteLine josephus(41,3,3) |
|||
</lang> |
|||
{{Out}} |
|||
<pre> |
|||
2 |
|||
30 |
|||
15,30,34 |
|||
</pre> |
</pre> |
||