3

I'm working on a function $\zeta(\xi)$ that takes as input an integer $\xi$ (in base $10$) and calculates how many $1$'s there are in its binary representation. How can I write such function?

Here is a graph of $\zeta(\xi)$, for $0\leq\xi\leq250$, created in Excel:

enter image description here

By now, I have tried only brute-forcing: calculate a lot of values of $\zeta(\xi)$ to try to find a common pattern, but this doesn't seem helpful in finding an explicit formula. Any idea?

Matteo
  • 6,581
  • 2
    What do you want out of this? Please try to explain more clearly what kind of answer you are hoping for. – TonyK Aug 17 '19 at 12:45
  • @ViktorGlombik: yes, an explicit formula for $\zeta(\xi)$. – Matteo Aug 17 '19 at 12:48
  • 2
    Explicit formula in terms of what? I still have no idea what you are looking for. You have already programmed it -- what more do you want? – TonyK Aug 17 '19 at 12:50
  • 1
    As you can see in the post I'm searching an explicit formula $\zeta(\xi)$ (if exist) that, starting from a natural number $\xi$ written in base $10$, counts how many $1$ are there in its binary rappresentation. – Matteo Aug 17 '19 at 12:53
  • Yes, it can be a very good way. But how can we count $0$s? – Matteo Aug 17 '19 at 13:11
  • 1
    I think if you want to get anything useful out of this, you have to post your "brute-force" method. – TonyK Aug 17 '19 at 13:18
  • 2
    Practically everything that is known will be in OEIS as it is quite an "old" sequence.. – Vepir Aug 17 '19 at 14:22
  • 1
    Note that it's totally irrelevant that they are initially written in base 10 (or 3, or variable base, whatever). They are numbers, and you are considering the binary (base 2) representation, period. – Jean-Claude Arbaut Aug 17 '19 at 15:10
  • Alternating Mersenne sum subtracting when 0 occurs, adding where the next 1 occurs is a way to represent a number's binary expansion. Not quite useful here though. –  Aug 17 '19 at 16:03

4 Answers4

3

For computer applications, there is hardware support for this problem:

Intel CPU Nvidia GPU

Here is an algorithm in C from Brian Kernighan:

uint64_t countBits(uint64_t input){
uint64_t counter;
for(counter=0; input > 0; counter++)
    input &= (input - 1); /*clear the set lsb*/
return counter;
}
2

Here you go: $$ f(n) = \begin{cases} 0 & n = 0 \\ 1 & n = 1 \\ n \bmod 2 + f(\lfloor \frac{n}{2} \rfloor) & \text{else} \end{cases} $$

This exactly matches @ViktorGlombik's observation in the comments (no surprise!) If you prefer, there's $$ f(n) = \sum_{i = 0}^n \left( \lfloor\frac{n}{2^i}\rfloor \right) \bmod 2. $$

And if you don't like the use of $k \bmod 2$, you can replace it by $k - 2\lfloor\frac{k}{2}\rfloor$ (which works for nonnegative $k$ at least, which appears to be what you care about).

And if you don't like the use of "floor", you can replace it by $$ \lfloor x \rfloor = x - \frac12 + \frac{1}{\pi} \sum_{k=1}^\infty \frac{\sin (2 \pi k x)}{k}. $$

But honestly --- all that work to express as a double-sum a thing for which you've already got a working program? It's hard to see the value here. If you want to do it, go for it.

John Hughes
  • 93,729
2

I'm pretty sure you want your function to take input as a binary number -- otherwise most of the work would lie in converting from a decimal representation to the machine's native binary.

In the absence of special hardware support for this operation (often known as popcout for binary "population count"), the usual trick is to squeeze a tree of parallel additions into a single word, by periodically masking out spurious bits that would produce carries between the sub-additions:

uint64 popcount(uint64 x) {
   x = (x & 0x5555555555555555L) + ((x >> 1) & 0x5555555555555555L);
   x = (x & 0x3333333333333333L) + ((x >> 2) & 0x3333333333333333L);
   x = x + (x >> 4);
   x = x & 0x0F0F0F0F0F0F0F0FL;
   x = x + (x >> 8);
   x = x + (x >> 16);
   x = x + (x >> 32);
   return x & 0xFF;
}
1

Specifically for binary base, the number of 1's is equel with the sum of the digits in base 2. E.g. $$\zeta(5_{10})=\zeta(101_{2})=1+0+1=s_2(5_{10})=2$$ And then, there is this formula and one that can be derived from this result. $$s_2(n)=n-\nu_2(n!)=n-\sum\limits_{k=1}\left \lfloor \frac{n}{2^k} \right \rfloor$$

By the way, zeta ($\zeta$) is too famous to be redefined.

rtybase
  • 16,907