# Subtracting integers represented as natural numbers

You can represent any integer, z,

…, -3, -2, -1, 0, 1, 2, 3, …

as a natural number,

0, 1, 2, 3, …

All infinitely many of them. Map nonnegative integers to even numbers 2z and negative numbers to odd numbers -2z – 1.

So 1 goes to 2, 2 goes to 4, 3 goes to 6, and so on, and -1 goes to 1, -2 goes to 3, -3 goes to 5. Zero stays put.

Now, I know what you’re wondering. How can we define subtraction in this world, assuming we already know how to do arithmetic on natural numbers?

I tried doing it a hilariously convoluted way using… linear regression 🙂 . R code over here.

The regression models “wrote” the case_when below automatically. I haven’t tried to simplify any of it but, e.g., the

0 + 0 * x_pz + 0 * y_pz

can probably safely be reduced to 0, and

1 + 1 * x_pz + 1 * y_pz

to

1 + x_pz + y_pz

```pz_sub <- function(x_pz, y_pz) {
x_abs_pz <- ifelse(even(x_pz), x_pz, x_pz + 1)
y_abs_pz <- ifelse(even(y_pz), y_pz, y_pz + 1)

case_when(
even(x_pz) & even(y_pz) & x_abs_pz < y_abs_pz ~
-1 + -1 * x_pz + 1 * y_pz ,
odd(x_pz) & odd(y_pz) & x_abs_pz > y_abs_pz ~
-1 + 1 * x_pz + -1 * y_pz ,
odd(x_pz) & odd(y_pz) & x_abs_pz < y_abs_pz ~
0 + -1 * x_pz + 1 * y_pz ,
odd(x_pz) & odd(y_pz) & x_abs_pz == y_abs_pz ~
0 + 0 * x_pz + 0 * y_pz ,
even(x_pz) & even(y_pz) & x_abs_pz == y_abs_pz ~
0 + 0 * x_pz + 0 * y_pz ,
even(x_pz) & even(y_pz) & x_abs_pz > y_abs_pz ~
0 + 1 * x_pz + -1 * y_pz ,
odd(x_pz) & even(y_pz) & x_abs_pz > y_abs_pz ~
0 + 1 * x_pz + 1 * y_pz ,
odd(x_pz) & even(y_pz) & x_abs_pz < y_abs_pz ~
0 + 1 * x_pz + 1 * y_pz ,
even(x_pz) & odd(y_pz) & x_abs_pz == y_abs_pz ~
0 + 2 * x_pz + 0 * y_pz ,
even(x_pz) & odd(y_pz) & x_abs_pz < y_abs_pz ~
1 + 1 * x_pz + 1 * y_pz ,
even(x_pz) & odd(y_pz) & x_abs_pz > y_abs_pz ~
1 + 1 * x_pz + 1 * y_pz ,
odd(x_pz) & even(y_pz) & x_abs_pz == y_abs_pz ~
1 + 2 * x_pz + 0 * y_pz
)
}```