Created
October 29, 2015 21:26
-
-
Save karapetyan/7f548e05476ff904ccb3 to your computer and use it in GitHub Desktop.
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
def josephus_survivor(n,k) | |
circle = (1..n).to_a | |
index = -1 | |
size = circle.size | |
until circle.size == 1 | |
if index + k <= size | |
index += k | |
circle.delete_at(index) | |
else | |
index = k - (size - index) | |
circle.delete_at(index) | |
size = circle.size | |
end | |
end | |
end | |
josephus_survivor(7,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment