Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,8 +1,18 @@
[[wp:Josephus problem|Josephus problem]] is a math puzzle with a grim description: <math>n</math> prisoners are standing on a circle, sequentially numbered from <math>0</math> to <math>n-1</math>. An executioner walks along the circle, starting from prisoner <math>0</math>, removing every <math>k</math>-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 <math>n=5</math> prisoners and <math>k=2</math>, 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.
[[wp:Josephus problem|Josephus problem]] is a math puzzle with a grim description: <math>n</math> prisoners are standing on a circle, sequentially numbered from <math>0</math> to <math>n-1</math>.
'''Task''' Given any <math>n, k > 0</math>, find out which prisoner will be the final survivor. In one such incident, there were 41 prisoners and every 3rd prisoner was being killed (<math>k=3</math>). 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?
An executioner walks along the circle, starting from prisoner <math>0</math>,
removing every <math>k</math>-th prisoner and killing him.
'''Extra''' The captors may be especially kind and let <math>m</math> survivors free, and Josephus might just have <math>m-1</math> friends to save. Provide a way to calculate which prisoner is at any given position on the killing sequence.
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed. <br>
For example, if there are <math>n=5</math> prisoners and <math>k=2</math>, 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.
'''Task''' Given any <math>n, k > 0</math>, find out which prisoner will be the final survivor. In one such incident, there were 41 prisoners and every 3rd prisoner was being killed (<math>k=3</math>).
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?
'''Extra''' The captors may be especially kind and let <math>m</math> survivors free,
and Josephus might just have <math>m-1</math> friends to save.
Provide a way to calculate which prisoner is at any given position on the killing sequence.
'''Notes'''
# You can always play the executioner and follow the procedure exactly as described, walking around the circle, counting (and cutting off) heads along the way. This would yield the complete killing sequence and answer the above questions, with a complexity of probably <math>O(kn)</math>. However, individually it takes no more than <math>O(m)</math> to find out which prisoner is the <math>m</math>-th to die.

View file

@ -1,16 +1,15 @@
import std.stdio, std.algorithm, std.array, std.string, std.range;
T pop(T)(ref T[] items, in size_t i) pure {
T pop(T)(ref T[] items, in size_t i) pure /*nothrow*/ @safe /*@nogc*/ {
auto aux = items[i];
items.remove(i);
items.length--;
items = items.remove(i);
return aux;
}
string josephus(in int n, in int k) pure {
string josephus(in int n, in int k) pure /*nothrow*/ @safe {
auto p = n.iota.array;
int i;
int[] seq;
immutable(int)[] seq;
while (!p.empty) {
i = (i + k - 1) % p.length;
seq ~= p.pop(i);
@ -21,7 +20,7 @@ string josephus(in int n, in int k) pure {
seq[0 .. $ - 1].chunks(20), seq[$ - 1]);
}
void main() {
void main() /*@safe*/ {
josephus(5, 2).writeln;
writeln;
josephus(41, 3).writeln;

View file

@ -15,7 +15,7 @@
n=41; /* number of alive prisoners */
nn=n; /* wrap around boundary */
w=3; /* killing count */
s=1; /* nuber of survivors */
s=1; /* number of survivors */
p=-1; /* start here */
Do Until(n=s); /* until one alive prisoner */
found=0; /* start looking */

View file

@ -0,0 +1,11 @@
>>>def josephus(n, k):
r = 0
for i in xrange(1, n+1):
r = (r+k)%i
return 'Survivor: %d' %r
>>> print(josephus(5, 2))
Survivor: 2
>>> print(josephus(41, 3))
Survivor: 30
>>>

View file

@ -0,0 +1,19 @@
def josephus(n, k):
a = list(range(1, n + 1))
a[n - 1] = 0
p = 0
v = []
while a[p] != p:
for i in range(k - 2):
p = a[p]
v.append(a[p])
a[p] = a[a[p]]
p = a[p]
v.append(p)
return v
josephus(10, 2)
[1, 3, 5, 7, 9, 2, 6, 0, 8, 4]
josephus(41, 3)[-1]
30