Skip to content

Instantly share code, notes, and snippets.

@TheXer
Created August 25, 2021 18:52
Show Gist options
  • Save TheXer/700ccb364220cabd12026dce626bde0c to your computer and use it in GitHub Desktop.
Save TheXer/700ccb364220cabd12026dce626bde0c to your computer and use it in GitHub Desktop.
Josephus problem solution in Python
# 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