Skip to content

Instantly share code, notes, and snippets.

@daboross
Last active December 29, 2015 10:39
Show Gist options
  • Save daboross/7658940 to your computer and use it in GitHub Desktop.
Save daboross/7658940 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
size, every_x, current = [int(x) for x in sys.argv[1:]]
alive, till_kill = [True] * size, 0
while True:
if alive[current]:
if sum(alive) == 1:
break
elif till_kill == 0:
alive[current] = False
till_kill = every_x - 1
else:
till_kill -= 1
current += 1 if current < size - 1 else 1 - size
print("Josephus should be at position {} to survive.".format(current))
#!/usr/bin/env python3
import sys
s, k, c = [int(x) for x in sys.argv[1:]]
a, tn = [True] * s, 0
while True:
if a[c]:
if sum(a) == 1: break
elif tn == 0: a[c] = False; tn = k - 1
else: tn -= 1
c += 1 if c < s - 1 else 1 - s
print("Josephus should be at position {} to survive.".format(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment