1

I've given a pairing function c = (x+y)(x+y) + x and asked to make fst and snd functions which return x and y, if c is given.

Values x,y,c are non-negative integers: 0,1,2,3...

The problem is, that I'm currently working with plus and multiply operations only, so sqrt is not allowed.

Is there a way to encode invert pairing function without sqrt?

This is an educational task, so if there is a way, don't tell me the actual way for a while, please.

  • 3
    I think this function alone cannot tell you $x$ and $y$, you are trying to get two unknowns from one known number. E.g. if $c=2$ you may have x=0 and $y=\sqrt2$ or $x=1$ and $y=0$. – Suzu Hirose Jul 13 '22 at 00:08
  • 1
    Yes, there's no way to invert this. – Jair Taylor Jul 13 '22 at 00:37
  • 1
    Are $x$ and $y$ required to be integers? – MJD Jul 13 '22 at 02:35
  • Is the goal to find one solution of $f(x,y) = c$, given $c$? That is indeed possible without square roots, only addition, multiplication and a constant. Of course the solution is not unique. – Robert Israel Jul 13 '22 at 02:43
  • 1
    It seems to that if we require both $x$ and $y$ to be non-negative integers then to a given $c$ of this form the solution for $x$ and $y$ is actually unique. This is because $(x+y)^2\le c<(x+y+1)^2$. Unfortunately the obvious formula for $x+y$ would then be $\lfloor\sqrt{c}\rfloor$ which is not on your list of available operations. – Jyrki Lahtonen Jul 13 '22 at 05:03
  • @SuzuHirose if c = 2, I can invert this as (1+0)*(1+0) + 1 = 2; sqrt(2) - 1 = 0 – nutella_eater Jul 13 '22 at 08:13
  • @JyrkiLahtonen what about non obvious formulas? Is there a way to find nearest square number without square root? – nutella_eater Jul 13 '22 at 08:16
  • I'm fairly sure it is possible by a recursive algorithm. But then you need some kind of division. Like this. Convergence speeds up if you can start from a reasonable approximation. Otherwise there will be a number of iterations roughly one half of the length of $c$ in bits. Alternatively you could fork an approximate square root. – Jyrki Lahtonen Jul 13 '22 at 08:54
  • Division isn't required for integer square root. Just trial and error. – MJD Jul 13 '22 at 12:10

0 Answers0