Created
August 25, 2021 18:52
-
-
Save TheXer/700ccb364220cabd12026dce626bde0c to your computer and use it in GitHub Desktop.
Josephus problem solution in Python
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
# This is a solution to the Josephus Problem. | |
# This snippet has three parts: | |
# 1. bin(num)[3:] converts an integer to a binary representation and we take the biggest bit in binary with slicing | |
# 3. + "1" add an one to the binary (that we took out previously). | |
# 4. int(num, 2) converts the binary back to decimal | |
def josephus_problem(num): | |
return int(bin(num)[3:] + "1", 2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment