Last active
December 17, 2015 07:39
-
-
Save jsvensson/5574824 to your computer and use it in GitHub Desktop.
Josephus problem solution in Ruby.
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(size, steps, survivors = 2) | |
list = *1..size | |
def list.[](i) | |
fetch(i % length) | |
end | |
puts "Population #{list.length}, #{steps} steps, #{survivors} survivors" | |
while list.length > survivors | |
list.delete(list[steps-1]) | |
list.rotate!(steps-1) | |
end | |
puts "Position of survivors: #{list}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment