Skip to content

Instantly share code, notes, and snippets.

@taniarascia
Created October 13, 2021 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taniarascia/f4936950f8fad08595461762dbd6fee9 to your computer and use it in GitHub Desktop.
Save taniarascia/f4936950f8fad08595461762dbd6fee9 to your computer and use it in GitHub Desktop.
Check if a number is odious
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