2

I want to write mathematically a formula that checks the amount of the digit $0$ on even and on odd position of a given number $N$.

So for example $N=2000$ has $2$ zeros on odd position and $1$ zero on even. Or if $N=51601$ then $0$ zeros on odd position and $1$ zero on even.

How do I write this mathematically, I have no clue how to write a loop that checks every digit of a number of the size $n$? Something like this: $$O=\sum\limits_{\substack{pos = 0\\\ x=0}}^{pos = n} \mathbf{1}_{odd}\qquad \text{and}\qquad E=\sum\limits_{\substack{pos = 0\\\ x=0}}^{pos = n} \mathbf{1}_{even}$$

Where $x$ is the digit at position $pos$. And the variables $O$ stand for $\#$ odd zeros and $E$ for $\#$ of even zeros.

Thank you for help

Edit: It would be nice if it also works for a binary representation like $N=100101$

3 Answers3

0

HINT

Repeatedly divide the number by 100 and ignore the reminder

  • 1
    this should be a comment –  Dec 28 '16 at 10:57
  • @A.Molendijk Well, it's not –  Dec 28 '16 at 10:58
  • The problem with this is, when I use my formula on a binary number (for checking the 0s), then I have to divide a binary number through a decimal number. –  Dec 28 '16 at 11:01
  • @Matriz What programming language do you use? –  Dec 28 '16 at 11:14
  • @EugenCovaci I don't use it for a programming language. Just for a formal mathematical description. –  Dec 28 '16 at 11:40
0

There are tons of ways to go about it. Let $n \in \mathbb{N}$ be a number and $d(n)$ the function that counts the amount of zeros on even digits. Denote the indicator function as $\mathcal{I}_A(x) = 1$ if $x \in A$ and $0$ otherwise. Then, $$d(n) = \sum_i \mathcal{I}_0\left(\frac{n}{10^{2i-1}} \mod 10 \right).$$ Example, then \begin{align} d(1300) & = \sum_i \mathcal{I}_0\left(\frac{n}{10^{2i-1}} \mod 10 \right) \\ & = \mathcal{I}_0\left( \frac{n}{10} \mod 10 \right) + \mathcal{I}_0 \left(\frac{n}{10^3} \mod 10 \right) \\ & = \mathcal{I}_0 (0) + \mathcal{I}_0(1) = 1.\end{align} For the amount of zeros on odd position the function can be expressed in a similar manner.

0

You should introduce and define your own notation.

Let $n$ be written as a base $10$ numeral as $d_k \ldots d_0$, where every $d_i \in \{ 0..9\}$ and $d_k \neq 0$. Then you can write $d_i(n)$ to denote the $i$th digit of $n$ and define e.g. the set of even positions of $n$ for which the digit at the position is $0$:

$$E_{0}(n) = \{ i \mid \exists j. d_{2j}(n) = 0 \}$$

Hans Hüttel
  • 4,271
  • Thank you very much. This was the answer I was looking for. It's clear and without any division operation, so I don't have to worry about the representation of the number. –  Dec 28 '16 at 13:27
  • A little question though. This notation $E_0(n)$ is just the set of even positions so for the amount of actual zeros I need to write $# E_0(n)$? –  Dec 28 '16 at 19:54
  • 1
    Yes, or $|E_0(n)|$. – Hans Hüttel Dec 28 '16 at 20:20