Created
August 28, 2018 06:21
-
-
Save SamAsEnd/fba6871d8f3933696bc09fd8f2c9ac56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=uCsD3ZGzMgE