2018-09-30 22:01:58 +00:00
---
id: 5a23c84252665b21eecc7ec5
2020-11-27 18:02:05 +00:00
title: Josephus problem
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302294
2021-01-13 02:31:00 +00:00
dashedName: josephus-problem
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2022-06-08 13:28:24 +00:00
Josephus problem is a math puzzle with a grim description: $n$ prisoners are standing on a circle, sequentially numbered from $0$ to $n-1$.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
An executioner walks along the circle, starting from prisoner $0$, removing every $k$-th prisoner and killing him.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
For example, if there are $n=5$ prisoners and $k=2$, the order the prisoners are killed in (let's call it the "killing sequence") will be 1, 3, 0, and 4, and the survivor will be #2 .
2020-11-27 18:02:05 +00:00
Given any $n, k > 0$, find out which prisoner will be the final survivor.
In one such incident, there were 41 prisoners and every 3< sup > rd< / sup > prisoner was being killed ($k=3$).
2018-09-30 22:01:58 +00:00
Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
Which number was he?
2020-11-27 18:02:05 +00:00
# --instructions--
2022-06-08 13:28:24 +00:00
Write a function that takes the initial number of prisoners and `k` as parameter and returns the number of the prisoner that survives.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
`josephus` should be a function.
```js
assert(typeof josephus == 'function');
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`josephus(30,3)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof josephus(30, 3) == 'number');
```
2018-09-30 22:01:58 +00:00
2021-05-16 18:04:07 +00:00
`josephus(30,3)` should return `28` .
2018-09-30 22:01:58 +00:00
```js
2021-05-16 18:04:07 +00:00
assert.equal(josephus(30, 3), 28);
2020-11-27 18:02:05 +00:00
```
2020-09-15 16:57:40 +00:00
2021-05-16 18:04:07 +00:00
`josephus(30,5)` should return `2` .
2020-11-27 18:02:05 +00:00
```js
2021-05-16 18:04:07 +00:00
assert.equal(josephus(30, 5), 2);
2020-11-27 18:02:05 +00:00
```
2021-05-16 18:04:07 +00:00
`josephus(20,2)` should return `8` .
2020-11-27 18:02:05 +00:00
```js
2021-05-16 18:04:07 +00:00
assert.equal(josephus(20, 2), 8);
2018-09-30 22:01:58 +00:00
```
2021-05-16 18:04:07 +00:00
`josephus(17,6)` should return `1` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-16 18:04:07 +00:00
assert.equal(josephus(17, 6), 1);
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-05-16 18:04:07 +00:00
`josephus(29,4)` should return `1` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-16 18:04:07 +00:00
assert.equal(josephus(29, 4), 1);
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
```js
function josephus(init, kill) {
}
```
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2019-03-06 13:04:30 +00:00
function josephus(init, kill) {
2021-05-16 18:04:07 +00:00
const arr = Array.from(Array(init).keys());
let curr = -1
while (arr.length > 1) {
curr = (curr + kill) % arr.length;
arr.splice(curr, 1);
curr--;
2018-09-30 22:01:58 +00:00
}
2021-05-16 18:04:07 +00:00
return arr[0];
2018-09-30 22:01:58 +00:00
}
```