MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / josephus

Function josephus

python3/Math and Geometry/josephus.py:1–8  ·  view source on GitHub ↗
(n: int, k: int)

Source from the content-addressed store, hash-verified

1def josephus(n: int, k: int) -> int:
2 # Base case: If there's only one person, the last person is person 0.
3 if n == 1:
4 return 0
5 # Calculate the position of the last person remaining in the reduced problem
6 # with 'n - 1' people. We use modulo 'n' to ensure the answer doesn't exceed
7 # 'n - 1'.
8 return (josephus(n - 1, k) + k) % n

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected