Skip to content

Instantly share code, notes, and snippets.

@SamAsEnd
Created August 28, 2018 06:21
Show Gist options
  • Save SamAsEnd/fba6871d8f3933696bc09fd8f2c9ac56 to your computer and use it in GitHub Desktop.
Save SamAsEnd/fba6871d8f3933696bc09fd8f2c9ac56 to your computer and use it in GitHub Desktop.
def JosephusProblemSimulator(n, debug=False):
people = list(range(1, n+1))
person = 1
kill = False
while len(people) > 1:
if people.index(person) + 1 > len(people) - 1:
nxtPerson = people[0]
else:
nxtPerson = people[people.index(person) + 1]
if kill:
people.remove(person)
kill = False
if debug: print('killed', person)
else:
if debug: print('skip', person , 'gonna kill', nxtPerson)
kill = True
if debug: print('--=>', people, '\n')
person = nxtPerson
return people[0]
def JosephusProblemFastCalc(n):
return int((bin(n)[2:] + '1')[1:], 2)
print(JosephusProblemSimulator(41, True))
print(JosephusProblemFastCalc(41))
@SamAsEnd
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment