Skip to content

Instantly share code, notes, and snippets.

@jsvensson
Last active December 17, 2015 07:39
Show Gist options
  • Save jsvensson/5574824 to your computer and use it in GitHub Desktop.
Save jsvensson/5574824 to your computer and use it in GitHub Desktop.
Josephus problem solution in Ruby.
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