1

So I have an example function:

$$f(a,b) = \frac 12 ((a+b)^2 + 3a + b)$$

It "encodes" two variables into one unique number.

Assuming a and b are always positive, how would you write a function to find two input variables of the "encoding" function? So basically, how would you write a function to "decode" the output of that "encoding" function?

TZakrevskiy
  • 22,980
jocopa3
  • 113
  • Clearly, two values of $a$ for a fixed $b$ will give us the same $f(a,b)$ – lab bhattacharjee Jul 20 '13 at 10:02
  • I think it's necessary to prove first that $f:\mathbb N_+^2\to \mathbb N_+$ is bijective, which is not evident at the first sight. – TZakrevskiy Jul 20 '13 at 11:30
  • Moreover, $f\ge 4$, so it's definitely not surjective (it's surjective on its image, though, but now we need to find this image). – TZakrevskiy Jul 20 '13 at 11:38
  • 1
    Surprisingly (to me), $f$ is injective on $\mathbf{N} \times \mathbf{N}$, though not surjective; the image consists of all integers greater than or equal to $4$ that are neither triangular nor one less than triangular, i.e., not of the form $n(n+1)/2$ or $n(n+1)/2 - 1$ with $n \geq 3$. I don't have a formula to recover $(a, b)$ from $f(a, b)$, but it may help to express $f(a, b)$ in terms of the variables $u = a + b$ and $v = a - b$, namely, $f(a, b) = (1/2)\bigl((u+1)^2 + v - 1\bigr)$. – Andrew D. Hwang Jul 20 '13 at 11:40
  • What if I put this into a different context. Instead of being just a function, pretend it is an "encrpytion algorithm," so that the phrase "Hello." turns into $f(72,101)+f(108,108)+f(111,46) = 15123+23544+12514 = 51181$ where the a and b values represent the decimal version of each character. Knowing it is possible to decode that number back into a phrase, how would you go about doing so? A more simple way to see it is "He" encodes into $f(72,101) = 15123$, how would you decode it back to (72,101) or "He" – jocopa3 Jul 20 '13 at 12:04
  • @AndrewD.Hwang, I think that your proof of the injectivity and the description of the image of $f$ would be a nice contribution here. – TZakrevskiy Jul 20 '13 at 13:00

3 Answers3

3

Let $\mathbb N_0=\{0,1,2,\ldots\}$ and $\mathbb N=\{1,2,3,\ldots\}$.

The mapping $(a,b)\mapsto f(a,b)$ is a bijection from $\mathbb N_0\times\mathbb N_0$ to $\mathbb N_0$. Hence it is also a bijection from $\mathbb N\times\mathbb N$ to $\mathbb N\setminus D$ where $D=\left\{\frac12n(n+i)\mid n\in\mathbb N,i\in\{1,3\}\right\}$ collects the images $f(a,0)=\frac12a(a+3)$ and $f(0,b)=\frac12b(b+1)$ for $a$ and $b$ in $\mathbb N$.

To see this and how to reconstruct $(a,b)$ from $f(a,b)$, assume that $a$ and $b$ are nonnegative and call $s$ the largest integer $k$ such that $k^2\leqslant2f$, which can also be defined by the inequalities $$ s^2\leqslant2f\leqslant s(s+2). $$ Note that $2f=(a+b)^2+3a+b$ implies $(a+b)^2\leqslant2f\lt(a+b+2)^2$ hence $s=a+b$ or $s=a+b+1$.

Case $s=a+b$: this means that $2f\leqslant(a+b)(a+b+2)$, that is, $3a+b\leqslant2a+2b$, that is, $a\leqslant b$. Then, $a+b=s$ and $3a+b=2f-s^2$ hence $a=f-\frac12s(s+1)$ and $b=\frac12s(s+3)-f$. Then the condition $a\leqslant b$ holds and $a$ and $b$ are indeed integers.

Case $s=a+b+1$: this means that $2f\geqslant(a+b+1)^2$, that is, $3a+b\geqslant2a+2b+1$, that is, $a\geqslant b+1$. Then, $a+b+1=s$ and $3a+b=2f-(s-1)^2$ hence $a=f-\frac12s(s-1)$ and $b=\frac12s(s+1)-f-1$. Then the condition $a\geqslant b+1$ holds and $a$ and $b$ are indeed integers.

The first case applies when $s(s+1)\leqslant2f\leqslant s(s+2)$, the second when $s^2\leqslant 2f\lt s(s+1)$. To sum up:

For every integer $f\geqslant0$, let $s$ denote the unique integer such that $s^2\leqslant2f\leqslant s^2+2s$.

  • If $s^2\leqslant 2f\leqslant s^2+s-1$, then $a=f-\frac12s(s-1)$ and $b=\frac12s(s+1)-f-1$ (additionally, in this case $a\geqslant b+1$).
  • If $s^2+s\leqslant2f\leqslant s^2+2s$, then $a=f-\frac12s(s+1)$ and $b=\frac12s(s+3)-f$ (additionally, in this case $a\leqslant b$).

Exemple: If $f=15123$ as in a comment, then $2f=30246$, $s=173$, $s^2+s=30102$, $2f\geqslant s^2+s$ hence we apply the second item above, which yields $a=15123-\frac12\cdot173\cdot174=72$ and $b=\frac12\cdot173\cdot176-15123=101$.

Did
  • 279,727
  • Let's take $f=6$, then $s=3$, and by your formulas $(a,b)$ is either $(0,3)$ (which would give $f=9$) or $(1/2, 3/2)$, which is not a couple of integers. – TZakrevskiy Jul 20 '13 at 12:57
  • @TZakrevskiy If $f=6$ then $s=3$ and $2f\geqslant s(s+1)$ hence the first case applies and gives $a=f-\frac12s(s+1)=0$, $b=\frac12s(s+3)-f=3$, which is correct. – Did Jul 20 '13 at 13:08
  • Sorry, my bad, misplaced the coefficients at $a$ and $b$. – TZakrevskiy Jul 20 '13 at 13:14
  • @TZakrevskiy No problem (and this might be my fault since a previous version of my answer had misprints). – Did Jul 20 '13 at 13:15
  • I noticed $s = floor(sqrt(2f))$, so that would take out having to know $a$ and $b$. (Sorry for bad formating, not used to StackExchange's formatting system). – jocopa3 Jul 20 '13 at 13:34
  • ?? The post explains an algorithm allowing to compute $(a,b)$ from $f$ only (hence I do not understand your "take out having to know a and b"). – Did Jul 20 '13 at 13:41
  • More proof of my hypothesis to find $s$: $\sqrt{2f} = 173.9137...$, thus $s = floor(\sqrt{2f}) = 173$. I tested this with different values and found it remains true so long as $f$ is positive. So maybe it is possible to "decode" an output back to it's original inputs without knowing anything about the inputs. As for $e = f(a,b) + f(c,d)$, it is impossible to decode $e$, so I'd need a different way to put the outputs together so that it is distinct. – jocopa3 Jul 20 '13 at 13:48
  • In your algorithm, you have to know $s$. To know $s$, you have to know $a+b$ or $a+b+1$, meaning you have to also know $a$ and $b$. My method of finding $s$ doesn't require knowing $a$ $b$. To put it short, I'm a programmer, and I tend to look at problems from a programming perspective. – jocopa3 Jul 20 '13 at 14:03
  • In short, your post helped me find what I wanted, which is why I pick it as the best answer. Thank you for your help, even though I haven't quite explained what I wanted or what I'm doing very well. – jocopa3 Jul 20 '13 at 14:31
  • Sorry but this is absolutely wrong. From a programming perspective I am rather baffled that you still pretend (even after being explained otherwise) that one needs to know $a+b$ or $a+b+1$. ONE DOES NOT. Once again, $f$ (AND $f$ ALONE) yields $s$, and $s$ and $f$ together yield $a$ and $b$. For example one does not need to know $a+b$ or $a+b+1$ to make the algorithm work starting from $f=15123$. – Did Jul 20 '13 at 20:45
  • I fatuously rushed through reading your solution, and ended up miss-reading part of it. I am sorry for the confusion. The code that I got from your solution still works, and for that I still thank you. – jocopa3 Jul 21 '13 at 05:55
0

Inverting a function isn't always possible, because $f$ might not be one-to-one (that is, if $f(a,b)=f(c,d)$, then it isn't always the case that $(a,b)=(c,d)$). For your example function, notice that: $$ f(1,0)=2=f(-4,7) $$ so if the output is $2$, which input do we want: $(1,0)$ or $(-4,7)$?

Adriano
  • 41,576
  • What if the two input values are always positive and anything with a negative number is an extraneous solution, could you "decode" from there? – jocopa3 Jul 20 '13 at 10:59
0

For posterity, here's my sketch that $f(a, b) = (1/2)\bigl((a + b)^2 + 3a + b\bigr)$ is injective on the set of pairs of positive integers and the image consists of all integers greater than or equal to $4$ that are neither triangular nor one less than triangular. (This conclusion duplicates Did's answer.)

Throughout, $a$ and $b$ denote (arbitrary) positive integers. Easy calculations show that $$f(a+1, b-1) = f(a, b) + 1,\qquad f(1, a+1) = f(a, 1) + 3.$$ In words, $f$ takes consecutive integer values on the "anti-diagonal segment" $(1, a), (2, a-1), \dots, (a, 1)$, and "the lowest value on the next segment skips two consecutive integers". An easy induction shows the omitted values are the triangular numbers (starting with $6$) and their predecessors.