2018-09-30 23:01:58 +01:00
---
title: Josephus problem
id: 5a23c84252665b21eecc7ec5
challengeType: 5
---
## Description
< section id = 'description' >
2019-03-06 22:04:30 +09:00
< a href = "https://en.wikipedia.org/wiki/Josephus problem" target = "_blank" > Josephus problem< / a > is a math puzzle with a grim description: $n$ prisoners are standing on a circle, sequentially numbered from $0$ to $n-1$.
2018-09-30 23:01:58 +01:00
An executioner walks along the circle, starting from prisoner $0$, removing every $k$-th prisoner and killing him.
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed.
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 .
Given any < big > $n, k > 0$< / big > , 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 (< big > $k=3$< / big > ).
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.
Which number was he?
< / section >
## Instructions
< section id = 'instructions' >
2019-03-06 22:04:30 +09: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 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-08 12:52:10 +01:00
- text: < code > josephus</ code > should be a function.
2018-10-20 21:02:47 +03:00
testString: assert(typeof josephus=='function','< code > josephus< / code > should be a function.');
2018-10-08 12:52:10 +01:00
- text: < code > josephus(30,3)</ code > should return a number.
2018-10-20 21:02:47 +03:00
testString: assert(typeof josephus(30,3)=='number','< code > josephus(30,3)< / code > should return a number.');
2018-10-08 12:52:10 +01:00
- text: < code > josephus(30,3)</ code > should return < code > 29</ code > .
2018-10-20 21:02:47 +03:00
testString: assert.equal(josephus(30,3),29,'< code > josephus(30,3)< / code > should return < code > 29< / code > .');
2018-10-08 12:52:10 +01:00
- text: < code > josephus(30,5)</ code > should return < code > 3</ code > .
2018-10-20 21:02:47 +03:00
testString: assert.equal(josephus(30,5),3,'< code > josephus(30,5)< / code > should return < code > 3< / code > .');
2018-10-08 12:52:10 +01:00
- text: < code > josephus(20,2)</ code > should return < code > 9</ code > .
2018-10-20 21:02:47 +03:00
testString: assert.equal(josephus(20,2),9,'< code > josephus(20,2)< / code > should return < code > 9< / code > .');
2018-10-08 12:52:10 +01:00
- text: < code > josephus(17,6)</ code > should return < code > 2</ code > .
2018-10-20 21:02:47 +03:00
testString: assert.equal(josephus(17,6),2,'< code > josephus(17,6)< / code > should return < code > 2< / code > .');
2018-10-08 12:52:10 +01:00
- text: < code > josephus(29,4)</ code > should return < code > 2</ code > .
2018-10-20 21:02:47 +03:00
testString: assert.equal(josephus(29,4),2,'< code > josephus(29,4)< / code > should return < code > 2< / code > .');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-06 22:04:30 +09:00
function josephus(init, kill) {
2018-09-30 23:01:58 +01:00
// Good luck!
}
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-03-06 22:04:30 +09:00
function josephus(init, kill) {
2018-09-30 23:01:58 +01:00
var Josephus = {
init: function(n) {
this.head = {};
var current = this.head;
for (var i = 0; i < n - 1 ; i + + ) {
current.label = i + 1;
current.next = {
prev: current
};
current = current.next;
}
current.label = n;
current.next = this.head;
this.head.prev = current;
return this;
},
kill: function(spacing) {
var current = this.head;
while (current.next !== current) {
for (var i = 0; i < spacing - 1 ; i + + ) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
current = current.next;
}
return current.label;
}
}
return Josephus.init(init).kill(kill)
}
```
< / section >