Skip to main content
3 of 3
added final comment
bbgodfrey
  • 62.1k
  • 18
  • 92
  • 160

FindRoot cannot provide answers to Q2N[x] == 26, because Q2N (and, equivalently, Q2N1) is not a differentiable function. However, a solution clearly exists, which is the whole point of Cantor Pairing. In fact, Solve provides the answer.

Solve[26 == (n + d + 1) (n + d)/2 + d && n > 0 && d > 0, {n, d}, Integers]
(* {{n -> 1, d -> 5}} *)

where n and d are shorthand for Numerator[x] and Denominator[x]. The result is as expected.

More difficult cases are

Q2N[.21]
(* 7481 *)
Solve[7481 == (n + d + 1) (n + d)/2 + d && n > 0 && d > 0, {n, d}, Integers]
(* {{n -> 21, d -> 100}} *)

Q2N[.213]
(* 737291 *)
Solve[737291 == (n + d + 1) (n + d)/2 + d && n > 0 && d > 0, {n, d}, Integers]
(* {{n -> 213, d -> 1000}} *)

Q2N[.2112]
(* 287528 *)
Solve[287528 == (n + d + 1) (n + d)/2 + d && n > 0 && d > 0, {n, d}, Integers]
(* {{n -> 132, d -> 625}} *)

again as expected. (N[132/625] is, indeed, precisely 0.2112.)

Plot of First 1000 Q2N

For completeness, the solutions for the first 1000 values of Q2N can be plotted.

ListPlot[Table[{i, n/d /. Flatten@Solve[i == (n + d + 1) (n + d)/2 + d && n > 0 && d > 0,
   {n, d}, Integers]}, {i, 1000}], AxesLabel -> {"Q2N[x]", x}]

enter image description here

Note that a solution does not exist for every i, although one does for most i (e.g., 913 of the first 1000).

bbgodfrey
  • 62.1k
  • 18
  • 92
  • 160