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

@ -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