Created
February 27, 2014 00:51
-
-
Save florida/9242048 to your computer and use it in GitHub Desktop.
My Solution to the Josephus Problem.
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 last_man_standing(total) | |
total_in_circle = *1..total | |
until total_in_circle.size == 1 | |
for i in (1..((total_in_circle.size.to_f / 2)).ceil) | |
if total_in_circle[i] == total_in_circle[total_in_circle.size - 2] | |
total_in_circle.delete_at i | |
total_in_circle.delete_at(0) | |
else | |
total_in_circle.delete_at i | |
end | |
end | |
end | |
puts total_in_circle[0] | |
end | |
(1..1000).each do |x| | |
last_man_standing x | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment