Writing mathematically a formula that checks every digit

77 Views Asked by At

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

There are 3 best solutions below

3
On BEST ANSWER

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 \}$$

5
On

HINT

Repeatedly divide the number by 100 and ignore the reminder

3
On

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.