Skip to content

Instantly share code, notes, and snippets.

@e-mihaylin
Created October 18, 2018 09:11
Show Gist options
  • Save e-mihaylin/a9b38c3317070392aebfa9a298cc5687 to your computer and use it in GitHub Desktop.
Save e-mihaylin/a9b38c3317070392aebfa9a298cc5687 to your computer and use it in GitHub Desktop.
//https://www.codewars.com/kata/596d34df24a04ee1e3000a25
const countOnes = (a, b) => cab(b) - cab(a - 1);
const cab = n => {
let bits = 0;
for (let i = 0; i < 30; ++i)
bits += ccb(i, n);
return bits;
}
const ccb = (col, n) => {
const div = (n + 1) / (2 << col);
const sum = Math.floor(div);
const rest = Math.max(0, (div - sum) * (2 << col) - (1 << col));
return sum * (1 << col) + rest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment