-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add Josephus Problem #10928
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
Merged
Merged
Add Josephus Problem #10928
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1c28ce3
Add Josephus Problem
aqib-m31 f0d0920
Add iterative implementation of Josephus Problem
aqib-m31 da302ee
Add descriptive variable names
aqib-m31 b50614a
Update maths/josephus_problem.py
cclauss b68f675
Merge branch 'TheAlgorithms:master' into josephus_problem
aqib-m31 7a42a24
Update josephus_problem.py
aqib-m31 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
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: | ||
""" | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.