Josephus Problem in TypeScript

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

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

Current Solution

function printUsage(): void {
  console.error(
    "Usage: please input the total number of people and number of people to skip.",
  );
  process.exit(1);
}

function josephus(n: number, k: number): number {
  if (n === 1) return 1;
  return ((josephus(n - 1, k) + k - 1) % n) + 1;
}

function main(): void {
  const n = Number.parseInt(process.argv[2], 10);
  const k = Number.parseInt(process.argv[3], 10);

  if (Number.isNaN(n) || Number.isNaN(k) || n <= 0 || k <= 0) {
    printUsage();
  }

  console.log(josephus(n, k).toString());
}

main();

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