Created
October 13, 2021 16:37
-
-
Save taniarascia/f4936950f8fad08595461762dbd6fee9 to your computer and use it in GitHub Desktop.
Check if a number is odious
This file contains hidden or 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
function isOdious(x) { | |
if (x < 0) throw new Error('Value must be positive!') | |
const binaryExpansion = x.toString(2) | |
const count = Array.from(binaryExpansion).reduce((acc, val) => (val == 1 ? acc + 1 : acc), 0) | |
return count % 2 === 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment