Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active October 29, 2016 17:24
Show Gist options
  • Save anddam/c2711c52160f7cba93ef1db9f6c9aa04 to your computer and use it in GitHub Desktop.
Save anddam/c2711c52160f7cba93ef1db9f6c9aa04 to your computer and use it in GitHub Desktop.
def josephus_problem_iterate(soldiers):
"Iterative solution to Josephus' problem"
while len(soldiers) > 1:
start = 2 if len(soldiers) % 2 else 0
soldiers = soldiers[start::2]
return soldiers
def josephus_problem_recurse(soldiers):
"Recursive solution to Josephus' problem"
if len(soldiers) <= 1:
return soldiers
start = 2 if len(soldiers) % 2 else 0
return josephus_problem_recurse(soldiers[start::2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment