question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Josephus Problem - Off By One

See original GitHub issue

Affected page https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/josephus-problem

Your code

function josephus(init, kill) {
  // Setup
  let currPrisoner = 0;
  const prisoners = Array(init).fill(true);
  // Execute prisoners
  console.log("Kill Sequence:");
  for (let numAlive = init; numAlive > 1; numAlive--) {
    // Find next prisoner
    for (let numSkip = kill; numSkip > 0; numSkip--) {
      do {
        currPrisoner = (currPrisoner + 1) % init;
      } while (!prisoners[currPrisoner]);
    }
    // Execute prisoner
    console.log(currPrisoner);
    prisoners[currPrisoner] = false;
  }
  // Return last man standing
  console.log("Last Man Standing:");
  console.log(prisoners.indexOf(true));
  return prisoners.indexOf(true);
}

josephus(5, 2);

Expected behavior The code that passes the test suite (given above) provides different output than the traditional description of the problem. I think the test suite has an ‘off by one’ error in initializing the starting position.

Current output with solution that passes test suite:

Kill Sequence:
2
4
1
0
Last Man Standing:
3

Expected output per problem description:

Kill Sequence:
1
3
0
4
Last Man Standing:
2

Relevant portion of problem description:

For example, if there are n=5 prisoners and k=2, the order the prisoners are killed in (let’s call it the “killing sequence”) will be 1, 3, 0, and 4, and the survivor will be 2.

See also discussion here: https://forum.freecodecamp.org/t/solution-to-rosetta-code-challenge-josephus-problem/460203

Proposed Fix Change test suite to

josephus(30,3) should return 28

josephus(30,5) should return 2

josephus(20,2) should return 8

josephus(17,6) should return 1

josephus(29,4) should return 1

Bug reported by jsdisco on the forum.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremyltcommented, May 17, 2021

I agree that the original solution is pretty convoluted.

1reaction
Sembaukecommented, May 16, 2021

@alirezaghey, You can always open a PR. After that, we can give feedback and suggest changes if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Josephus problem - Wikipedia
In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game.
Read more >
Josephus Problem - GeeksforGeeks
In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which...
Read more >
The Josephus Problem - Medium
…is a famous puzzle in computer science and mathematics, related to a certain counting-out game. From Wikipedia: People are standing in a circle...
Read more >
Josephus Problem - InterviewBit
Recursive Approach. A simple approach to solve this problem is to find the position of the step which would be called after each...
Read more >
The Josephus Problem - Numberphile - YouTube
The Josephus Problem, featuring Daniel Erman from University of Wisconsin-Madison.Winning at Dots and Boxes: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found