Skip to content

Instantly share code, notes, and snippets.

@goofballLogic
Last active June 29, 2026 12:26
Show Gist options
  • Select an option

  • Save goofballLogic/bcb9451362a0c9dc68adf3106facf6a0 to your computer and use it in GitHub Desktop.

Select an option

Save goofballLogic/bcb9451362a0c9dc68adf3106facf6a0 to your computer and use it in GitHub Desktop.
not entirely sure i got the math spot on, and I'm fairly sure I can do this with a logarithm if I thought hard enough but...
function meanBits(n) {
let bits = 1, counted = 0, sum = 0;
while(counted < n) {
const depth = Math.pow(2, Math.max(1, bits - 1));
const count = Math.min(n - counted, depth);
sum += count * bits;
counted += count;
bits++;
}
return Math.round(sum / n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment