Created
October 18, 2018 09:11
-
-
Save e-mihaylin/a9b38c3317070392aebfa9a298cc5687 to your computer and use it in GitHub Desktop.
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
//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