Created
July 10, 2020 18:34
-
-
Save cortezcristian/0840f5f6c7080c642b1790fc7ce19cb0 to your computer and use it in GitHub Desktop.
Coding the Josephus Problem solution3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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