3

I want to simplify the following:

$\sum_{n=0}^x 1 - (1 - I[a \ne 0] * I[a \ge b]) * (1 - I[b \ne 0] * I[b \ge c]) * (1 - I[c \ne 0] * I[c \ge d]) * (1 - I[d = 0])$

user64742
  • 2,207
  • 1
    Please clarify, so you have sorted the array and eliminated all the duplicates? And you don't want to waste the space left by the eliminated duplicates? – user_194421 Jul 12 '16 at 02:01
  • But why did you make it a 4d array? – user_194421 Jul 12 '16 at 02:18
  • Is $x$ the number of non-duplicates? – user_194421 Jul 12 '16 at 02:19
  • So $I[x]$ means the $x$th index? If so, what does the $<$ in your formula mean? – user_194421 Jul 12 '16 at 02:24
  • OK, I'll try to solve it. – user_194421 Jul 12 '16 at 02:52
  • Writing each index $n$ as a four digit number $abcd$, then , your indicator sum looks like: $$J(x) = \sum_{n=0}^x I[a < b < c < d]$$ -correct? Also, zero based presumably to get the 10 digits, so J(123) =1 and all smaller values are zero? – Joffan Jul 12 '16 at 13:45
  • I understood that digits can be zero... but I didn't think equal digits were permitted. Also I don't understand how both 21 and 23 are possible. – Joffan Jul 12 '16 at 15:03
  • @TheGreatDuck: Your post is hard to understand. it seems that you don't know what an indicator function is. "It returns 1 when true and 0 when false." is wrong. $I[a \ne 0]$ and $I[a \ge b]$ does not make sense. Please explain the meaning of $I[a \ne 0]$ and $I[a \ge b]$. I don't understand " There are no indices where they are equal" Can yoü explain this? Instead of starting a bounty it would be better to improve your question. – miracle173 Jul 12 '16 at 19:28
  • Is $x$ still the argument? – user_194421 Jul 12 '16 at 22:53
  • So all multiples of $10_y$ generate $1$? – user_194421 Jul 12 '16 at 23:25
  • but that would cause $I[d=0]$ to equal $0$. (by the way, I suggested an edit.) – user_194421 Jul 12 '16 at 23:35
  • I suggest moving this to chat. Having $21$ comments in a single post is not good practice. – user_194421 Jul 12 '16 at 23:38
  • Maybe you tried to edit overriding my suggestion? – user_194421 Jul 12 '16 at 23:40
  • I mean it's (1-I[d=0]) that becomes 0. The expressions in the parentheses will be multiplied by 0. – user_194421 Jul 12 '16 at 23:41
  • ok (again, I suggest moving this to chat. The current comment count is 28) – user_194421 Jul 12 '16 at 23:42
  • @TheGreatDuck The link in your comment confirms that your terminology is wrong. – miracle173 Jul 13 '16 at 00:43
  • Are you able to rephrase this as a counting problem rather than obscuring your intent by packing into some irrelevant multidimensional array? For instance, "I want the counting function $J(n)$ for $n \in \Bbb{Z}{>0}$ which counts the number of elements in the set $\bigcup{k=0}^{10} {a_k 10^k + a_{k-1}10^{k-1} + \cdots + a_0 \mid a_k < a_{k-1} < a_{k-2} < \cdots < a_0}$ which are less than or equal to $n$"? It would be far more informative to have a clear wordy description of what you want than try to guess what you want from formulas. – Eric Towers Jul 13 '16 at 14:41
  • @EricTowers $k$ would go up to $3$, no $10$. And if you read carefully, the array has been abstracted away, the only remnants are 4-digit numbers which correspond to each member of the array. – user_194421 Jul 13 '16 at 15:14
  • @user351579 : Given the edit history of the problem, it seems clear that the OP doesn't know whether his summation actually represents the set he wants. For instance, the current summation counts $0$ and the comments above have shown the OP has not always been clear on whether $f$ counts $0$ or not. Regarding "$k$ would go up to $3$...", OP has been semi-clear that this problem is actually posed for additional digit lengths and additional bases. – Eric Towers Jul 13 '16 at 15:18
  • @EricTowers The expression in the question, $1-(1-I[a\neq0]I[a\ge b])(1-I[b\neq0]I[b\ge c])(1-I[c\neq0]I[c\ge d])(1-I[d=0])$, will evaluate to $0$ if and only if $x$ is a non-zero $z$-digit number (i.e. the most significant digit of $x$ is the $z$th from the right) such that all $z$ digits are in ascending order. – user_194421 Jul 13 '16 at 15:38
  • @EricTowers The OP did intent the expression to equal $1$ if and only if the number is skipped. He did say the first values skipped would be $0,10,11,20,21,22,...$, which is precisely the first values which will not generate $0$ according to my previous comment. – user_194421 Jul 13 '16 at 15:43
  • @user351579 : The OP eventually came to that conclusion (counting $0$). If it isn't transparently clear to the OP that their expression counts what they want, I have no confidence that it does. Stating counting problems as counting problems is the non-self-defeating choice. – Eric Towers Jul 13 '16 at 15:45

1 Answers1

3

Let $x$ be a base $y$ non-negative integer with at most $4$ digits. Let $a$, $b$, $c$, $d$ such that $0\le a,b,c,d<y$ and the following equations hold true: $$\left\{\begin{aligned} a&=\lfloor\frac{n}{1000}\rfloor\ mod\ 10 \\[2ex]b&=\lfloor\frac{n}{100}\rfloor\ mod\ 10 \\[2ex]c&=\lfloor\frac{n}{10}\rfloor\ mod\ 10 \\[2ex]d&=\lfloor n\rfloor\ mod\ 10 \end{aligned}\right.$$

The expression in the question, $1-(1-I[a\neq0]I[a\ge b])(1-I[b\neq0]I[b\ge c])(1-I[c\neq0]I[c\ge d])(1-I[d=0])$, will evaluate to $0$ if and only if $x$ is a non-zero $z$-digit number (i.e. the most significant digit of $x$ is the $z$th from the right) such that all $z$ digits are in ascending order. Otherwise, it will evaluate to $1$.

Let all the possible values of $x$ for which the expression evaluates to $1$ be called skipped numbers, and let all the possible values of $x$ for which the expression evaluates to $0$ be called non-skipped numbers.

For every non-skipped $x$, let $g(x)$ be the number of non-skipped numbers up to $x$.

$g(x)$ can then be described as follows:

$g(x)=\left\{\begin{aligned} 0\le x\le y-1\to\ &x \\y\le x\le y^2-1\to\ &\binom{y-1}1+ \sum_{i=y-c}^{y-2}\binom i1+(d-c) \\y^2\le x\le y^3-1\to\ &\binom{y-1}1+\binom{y-1}2+ \sum_{i=y-b}^{y-2}\binom i2+ \sum_{i=y-c}^{y-b-2}\binom i1+(d-c) \\y^3\le x\le y^4-1\to\ &\binom{y-1}1+\binom{y-1}2+\binom{y-1}3\\&+ \sum_{i=y-a}^{y-2}\binom i3+ \sum_{i=y-b}^{y-a-2}\binom i2+ \sum_{i=y-c}^{y-b-2}\binom i1+(d-c) \end{aligned}\right.$

For every integer $n\ge0$, let there be a function $h_n:\{x|x\in\mathbb Z,x\ge n\}\to\mathbb Z$ such that $h_n(x)=\sum_{i=n}^x\binom in$. Then $g$ can be restated as follows:

$g(x)=\left\{\begin{aligned} 0\le x\le y-1\to\ &x \\y\le x\le y^2-1\to\ &\binom{y-1}1\\&+ h_1(y-2)-h_1(y-c-1)+(d-c) \\y^2\le x\le y^3-1\to\ &\binom{y-1}1+\binom{y-1}2\\&+ h_2(y-2)-h_2(y-b-1)\\&+ h_1(y-b-2)-h_1(y-c-1)+(d-c) \\y^3\le x\le y^4-1\to\ &\binom{y-1}1+\binom{y-1}2+\binom{y-1}3\\&+ h_3(y-2)-h_3(y-a-1)\\&+ h_2(y-a-2)-h_2(y-b-1)\\&+ h_1(y-b-2)-h_1(y-c-1)+(d-c) \end{aligned}\right.$

It can be shown that $h_n(x)=\binom xn+\binom {x-1}n+...+\binom nn=\binom{x+1}{n+1}$. Then the function can be restated again as follows:

$g(x)=\left\{\begin{aligned} 0\le x\le y-1\to\ &x \\y\le x\le y^2-1\to\ &\binom{y-1}1+ \binom{y-1}2-\binom{y-c}2+(d-c) \\y^2\le x\le y^3-1\to\ &\binom{y-1}1+\binom{y-1}2+ \binom{y-1}3-\binom{y-b}3\\&+ \binom{y-b-1}2-\binom{y-c}2+(d-c) \\y^3\le x\le y^4-1\to\ &\binom{y-1}1+\binom{y-1}2+\binom{y-1}3+ \binom{y-1}4-\binom{y-a}4\\&+ \binom{y-a-1}3-\binom{y-b}3+ \binom{y-b-1}2-\binom{y-c}2\\&+(d-c) \end{aligned}\right.$

The following will be turned into polynomial form in order:

  1. all instances of $\binom{y-1}1+\binom{y-1}2$, into $\frac{y(y-1)}2$
  2. the only instance of $\binom{y-1}3+\binom{y-1}4$, into $\frac{y(y-1)(y-2)(y-3)}{24}$
  3. the remaining $\binom{y-1}3$, into $\frac{(y-1)(y-2)(y-3)}6$
  4. all instances of $-\binom{y-c}2+(d-c)$, into $\frac{c((2y-3)-c)}2+d-\frac{y(y-1)}2$

Then the function description is rearranged as follows:

$g(x)=\left\{\begin{aligned} 0\le x\le y-1\to\ &x \\y\le x\le y^2-1\to\ & \frac{y(y-1)}2+\frac{c((2y-3)-c)}2+d-\frac{y(y-1)}2 \\y^2\le x\le y^3-1\to\ & \frac{y(y-1)}2+\frac{(y-1)(y-2)(y-3)}6\\& +\frac{c((2y-3)-c)}2+d-\frac{y(y-1)}2\\& +\binom{(y-1)-b}2-\binom{y-b}3 \\y^3\le x\le y^4-1\to\ & \frac{y(y-1)}2+\frac{y(y-1)(y-2)(y-3)}{24}\\& +\frac{c((2y-3)-c)}2+d-\frac{y(y-1)}2\\& +\binom{(y-1)-b}2-\binom{y-b}3\\& +\binom{(y-1)-a}3-\binom {y-a}4 \end{aligned}\right.$

Consider that every instance of $\frac{y(y-1)}2$ and $-\frac{y(y-1)}2$ can be cancelled out, leaving the final function descriptor:


$$g(x)=\left\{\begin{aligned} 0\le x\le y-1\to\ &x \\y\le x\le y^2-1\to\ & \frac{c((2y-3)-c)}2+d \\y^2\le x\le y^3-1\to\ & \frac{(y-1)(y-2)(y-3)}6 +\frac{c((2y-3)-c)}2+d\\& +\binom{(y-1)-b}2-\binom{y-b}3 \\y^3\le x\le y^4-1\to\ & \frac{y(y-1)(y-2)(y-3)}{24} +\frac{c((2y-3)-c)}2+d\\& +\binom{(y-1)-b}2-\binom{y-b}3 +\binom{(y-1)-a}3-\binom {y-a}4 \end{aligned}\right.$$


For every integer $x\ge0$, let $f(x)$ be the number of skipped numbers up to $x$, and for every integer $x>0$, let $j(x)$ be the highest non-skipped number not larger than $x$. In the special case that $x=0$, the value of $j(x)$ is $0$.

By definition of $g$,

$$f(x)=x+1-g(j(x))\text.$$


Notes:
1. $g(x)$ can be stated witout binomials. I left some binomials as they are because the polynomials they turn into are too complex.
2. You can simplify $g(x)$ somewhat by plugging in the value of $y$ yourself and performing some pre-calculation, or you can let it be and make the compiler do the pre-calculation for you.
3. $g(x)$ is only intended to produce the expected result if $x$ is non-skipped. That is why I defined $j$.

user_194421
  • 2,291
  • @TheGreatDuck So the $x$ in your expression is a function argument? So $f(x)$ is the number of numbers $n=\overline{abcd}$ such that $n\le x$ and $a<b<c<d$? – user_194421 Jul 12 '16 at 03:45
  • @TheGreatDuck What formula are you trying to simplify then? – user_194421 Jul 12 '16 at 03:48
  • So you're trying to simplify that $f(x)$? – user_194421 Jul 12 '16 at 04:50
  • So the $n$ in my comment is a base 256 number in your program? If so, I'll try again with updated information. – user_194421 Jul 12 '16 at 04:55
  • Can you sacrifice memory for speed? If so, I have a solution (You only need to iterate once, in subsequent function calls you use the result of the iteration) – user_194421 Jul 12 '16 at 05:18
  • Okay, I'll try again. (This might take some time) – user_194421 Jul 12 '16 at 05:20
  • @TheGreatDuck I have edited the answer. – user_194421 Jul 13 '16 at 12:32
  • @TheGreatDuck I have edited the answer again. – user_194421 Jul 13 '16 at 16:10
  • I was contemplating something like this. I'm glad someone wrote it up. For each number of digits, the formula is a polynomial in $a$, $b$, $c$, and $d$. You can expand and recombine the binomials a bit further, but I don't think any answer can get very much simpler than this and still solve the problem as stated. I'd accept it if it were my question. – David K Jul 13 '16 at 19:57
  • Good thinking. There is no reason to rush. Also, I realized after posting that I had just repeated a lot of what you wrote, a hazard of posting from a phone a bit at a time while on the run. – David K Jul 14 '16 at 00:36
  • @TheGreatDuck I have simplified the answer. – user_194421 Jul 14 '16 at 05:23
  • @TheGreatDuck The simplified answer contains only $7$ terms for the 4-digit case. – user_194421 Jul 16 '16 at 05:23