Skip to content

Instantly share code, notes, and snippets.

@cortezcristian
Created July 10, 2020 18:34
Show Gist options
  • Save cortezcristian/0840f5f6c7080c642b1790fc7ce19cb0 to your computer and use it in GitHub Desktop.
Save cortezcristian/0840f5f6c7080c642b1790fc7ce19cb0 to your computer and use it in GitHub Desktop.
Coding the Josephus Problem solution3.js
// Third solution binary based
const solution3 = (n) => {
// Convert to BIN
const binaryStr = Number(n).toString(2);
// Swapping digits
const newBinaryStr = (binaryStr + binaryStr[0]).replace(/./, '');
// Convert back to DEC
return parseInt(newBinaryStr, 2);
};
const position = solution3(5);
console.log(position); // >>> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment