Josephus problem: Difference between revisions

no edit summary
m (Emacs Lisp: Improve formatting)
No edit summary
Line 4,133:
Prisoner killing order: 3 6 9 12 15 18 21 24 27 30 33 36 39 1 5 10 14 19 23 28 32 37 41 7 13 20 26 34 40 8 17 29 38 11 25 2 22 4 35 16
Survivor: 31</pre>
 
=={{header|Vlang}}==
{{trans|Go}}
<lang vlang>// basic task fntion
fn final_survivor(n int, kk int) int {
// argument validation omitted
mut circle := []int{len: n, init: it}
k := kk-1
mut ex_pos := 0
for circle.len > 1 {
ex_pos = (ex_pos + k) % circle.len
circle.delete(ex_pos)
}
return circle[0]
}
// extra
fn position(n int, kk int, p int) int {
// argument validation omitted
mut circle := []int{len: n, init: it}
k := kk-1
mut pos := p
mut ex_pos := 0
for circle.len > 1 {
ex_pos = (ex_pos + k) % circle.len
if pos == 0 {
return circle[ex_pos]
}
pos--
circle.delete(ex_pos)
}
return circle[0]
}
fn main() {
// show basic task fntion on given test case
println(final_survivor(41, 3))
// show extra fntion on all positions of given test case
println("Position Prisoner")
for i in 0..41 {
println("${i:5}${position(41, 3, i):10}")
}
}
</lang>
 
{{out}}
<pre>
30
Position Prisoner
0 2
1 5
2 8
3 11
4 14
5 17
6 20
7 23
8 26
9 29
10 32
11 35
12 38
13 0
14 4
15 9
16 13
17 18
18 22
19 27
20 31
21 36
22 40
23 6
24 12
25 19
26 25
27 33
28 39
29 7
30 16
31 28
32 37
33 10
34 24
35 1
36 21
37 3
38 34
39 15
40 30
</pre>
 
 
=={{header|Wren}}==
338

edits