You should write a program or function which takes a non-negative integer N
as input and outputs or returns two integers (negative, zero or positive) X
and Y
.
Integers are meant in the mathematical sense as there are infinitely many of them.
The implemented function has to be bijective. This means that for every N
it has to output a different X
Y
pair and every X
Y
pair should be outputted for some input N
i.e. all of the following pairs should be outputted for some N
:
...
┌─────┬─────┬────┬────┬────┐
│-2 -2│-2 -1│-2 0│-2 1│-2 2│
├─────┼─────┼────┼────┼────┤
│-1 -2│-1 -1│-1 0│-1 1│-1 2│
├─────┼─────┼────┼────┼────┤
... │0 -2 │0 -1 │0 0 │0 1 │0 2 │ ...
├─────┼─────┼────┼────┼────┤
│1 -2 │1 -1 │1 0 │1 1 │1 2 │
├─────┼─────┼────┼────┼────┤
│2 -2 │2 -1 │2 0 │2 1 │2 2 │
└─────┴─────┴────┴────┴────┘
...
Note that U V
and V U
are different pairs if U!=V
.
Details
- If your language doesn't support arbitrarily large integers that's fine but your algorithm should work with an arbitrarily large integer data-type. Your code should still support input values for at least
2^31-1
. - If you choose to print or return the output as string no leading
0
's or+
signs are allowed. Otherwise your language's standard integer representation is fine.
Example
If the task would be to make a bijective function taking a non-negative integer N
and output one integer X
a solution could be the function
if (input mod 2 == 0) return N/2 else return -(N+1)/2
,
implemented in some language. This function returns X = 0 -1 1 -2 2...
for N = 0 1 2 3 4...
.
10=>11 12, 9=>10 11
is this invalid because 11 is repeated? \$\endgroup\$