8

Given two digit-strings $A$ and $B$, let $AB$ be their concatenation. So for example, if $A = ``102"$ and $B = ``101"$, $AB = ``102101"$.

We then say $AB \geq BA$ since $102101 \geq 101102$.

Now given digit strings $A$, $B$, and $C$, is it true that $$AB \geq BA \land BC \geq CB \Rightarrow AC \geq CA$$?


Context: This page gives 5 "simple" programming exercises. One of them is

Given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021.

My solution (and his solution) was to order the strings using the above comparison. This appears to work, but I'd like to prove it. I was able to come close to a proof, but it relies on the above comparison being a total preorder.

I've tried to prove the above transitivity relation in various ways, but have failed. A computer simulation of the first 100,000 numbers seems to imply it's true, though.

darij grinberg
  • 16,217
  • 4
  • 41
  • 85

1 Answers1

9

Let's denote the length (number of digits) in $A$ by $LA$ (and so on).

The first inequality you have is:

$A\times 10^{LB} +B \geq B\times 10^{LA} +A$

which can be rephrased as

$A\times(10^{LB}-1) \geq B\times(10^{LA}-1)$

or as

$A \geq B\times\frac{10^{LA}-1}{10^{LB}-1}$

Similarly, the second inequality can be rephrased as

$B \geq C\times\frac{10^{LB}-1}{10^{LC}-1}$

and the one you want to prove as

$A \geq C\times\frac{10^{LA}-1}{10^{LC}-1}$,

which now follows directly from the assumptions:

$A \geq B\times\frac{10^{LA}-1}{10^{LB}-1} \geq C\times\frac{10^{LB}-1}{10^{LC}-1}\times\frac{10^{LA}-1}{10^{LB}-1}=C\times\frac{10^{LA}-1}{10^{LC}-1}$.

(I implicitly assumed that none of these strings is of zero length)

  • The last bit then is that $A(10^{LC}-1)\geq C(10^{LA-1})$ which is the same as $A\times 10^{LC}+C \geq C\times 10^{LA}+A$, so $AC\geq CA$. Nice! – TravisJ May 14 '15 at 11:50
  • I used this inequality to speed up the "computer simulation" I mentioned, but I didn't even think to use it in a proof - do'h! Nice job!! – BlueRaja - Danny Pflughoeft May 14 '15 at 13:03
  • 1
    In a bit different notation: the relation $AB \geq BA$ is the same as $\frac{A}{10^{LA}-1} \geq \frac{B}{10^{LB}-1}$ which is clearly transitive. I wonder if there is a more abstract version of the proof, e.g. where $A,B$ are elements of an ordered group and this is an inequality on commutators. – sdcvvc May 24 '15 at 00:16
  • 1
    This is nice!! The result is a known lemma in combinatorics on words; e.g., I used it in my writeup of Hazewinkel's proof of the fact that the quasisymmetric functions form a polynomial ring (see Corollary 6.1.6 in [Darij Grinberg and Victor Reiner, *Hopf algebras in combinatorics*](http://www.cip.ifi.lmu.de/~grinberg/algebra/HopfComb-sols.pdf), which states it using arbitrary letters rather than digits, but this is clearly equivalent since we can WLOG assume the alphabet is $\left\{0,1,\ldots,k\right\}$ for some $k > 1$, and replace your $10$ by $k$, and observe that lexicographic order ... – darij grinberg Dec 12 '17 at 17:13
  • 1
    ... coincides with numerical order in the case of $AB \geq BA$ since the words $AB$ and $BA$ have the same length). My proof is purely combinatorial, but it is messy and unintuitive, whereas yours is clear and nice. Notice that the observation @sdcvvc made, that $AB \geq BA$ is equivalent to $\dfrac{A}{10^{LA}-1} \geq \dfrac{B}{10^{LB}-1}$, can be rewritten in the terminology of words as follows: $AB \geq BA$ is equivalent to $AAA \cdots \geq BBB \cdots$ (infinite words, lexicographic order). This fact (essentially Exercise 6.1.11 in loc. cit., but I don't use infinite words, ... – darij grinberg Dec 12 '17 at 17:17
  • ... so I have to truncate them at a convenient point) I have seen used in the literature on the Burrows-Wheeler transform. – darij grinberg Dec 12 '17 at 17:18