Josephus Problem in Haskell

Published on 11 May 2026 (Updated: 11 May 2026)

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

Current Solution

module Main where

import Text.Read
import System.Environment

josephus :: Int -> Int -> Int
josephus 1 _ = 1
josephus n k = (josephus (n-1) k + k - 1) `mod` n + 1

parseArgs :: [String] -> Maybe (Int, Int)
parseArgs [n, k] = do
  n' <- readMaybe n
  k' <- readMaybe k
  return (n', k')
parseArgs _ = Nothing

main :: IO ()
main = do
  args <- getArgs
  case parseArgs args of
    Just (n, k) -> print (josephus n k)
    Nothing     -> putStrLn "Usage: please input the total number of people and number of people to skip."

Josephus Problem in Haskell 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.