Josephus Problem in Ruby

Published on 15 February 2025 (Updated: 07 May 2026)

Welcome to the Josephus Problem in Ruby page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

def josephus(n, k)
  (1..n).reduce(0) { |acc, i| (acc + k) % i } + 1
end

def usage!
  abort("Usage: please input the total number of people and number of people to skip.")
end

n, k = ARGV

usage! unless n && k
usage! unless n.match?(/\A\d+\z/) && k.match?(/\A\d+\z/)

n = n.to_i
k = k.to_i

puts josephus(n, k)

Josephus Problem in Ruby was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.