Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Josephus Problem #10928

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions maths/josephus_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
The Josephus problem is a famous theoretical problem related to a certain
counting-out game. This module provides functions to solve the Josephus problem
for n people and a step size of k.

The Josephus problem is defined as follows:
- n people are standing in a circle.
- Starting with a specified person, you count around the circle,
skipping a fixed number of people (k).
- The person at which you stop counting is eliminated from the circle.
- The counting continues until only one person remains.

For more information about the Josephus problem, refer to:
https://en.wikipedia.org/wiki/Josephus_problem
"""


def josephus_recursive(n: int, k: int) -> int:
cclauss marked this conversation as resolved.
Show resolved Hide resolved
"""
Solve the Josephus problem for n people and a step size of k recursively.

Args:
n (int): Number of people.
k (int): Step size for elimination.

Returns:
int: The position of the last person remaining.

Examples:
>>> josephus_recursive(7, 3)
3
>>> josephus_recursive(10, 2)
4
"""
if n == 1:
return 0

return (josephus_recursive(n - 1, k) + k) % n


def find_winner(n: int, k: int) -> int:
cclauss marked this conversation as resolved.
Show resolved Hide resolved
"""
Find the winner of the Josephus problem for n people and a step size of k.

Args:
n (int): Number of people.
k (int): Step size for elimination.

Returns:
int: The position of the last person remaining (1-based index).

Examples:
>>> find_winner(7, 3)
4
>>> find_winner(10, 2)
5
"""
return josephus_recursive(n, k) + 1


def josephus_iterative(n: int, k: int) -> int:
cclauss marked this conversation as resolved.
Show resolved Hide resolved
"""
Solve the Josephus problem for n people and a step size of k iteratively.

Args:
n (int): The number of people in the circle.
k (int): The number of steps to take before eliminating someone.

Returns:
int: The position of the last person standing.

Examples:
>>> josephus_iterative(5, 2)
3
>>> josephus_iterative(7, 3)
4
"""
circle = list(range(1, n + 1))
current = 0

while len(circle) > 1:
current = (current + k - 1) % len(circle)
circle.pop(current)

return circle[0]


if __name__ == "__main__":
import doctest

doctest.testmod()