Find nearest whole integer(s) on function

256 Views Asked by At

I have two functions as follows:

$$a=\frac{5}{16}(x+2y)$$ $$b=-\frac{5}{16}(x-2y)$$

The user may enter any arbitrary number for (x, y). However only values which produce whole integers of a and b are considered valid. Given these conditions, is it possible to find the nearest valid (x, y) to the user's inputted x and y which would produce whole numbers for a and b.

I thought representing both equations as planes and finding the closest point on them to a specified point using Lagrange multiplication. But wasn't sure exactly how to do so, to produce the results I was looking for.

Edit: Follow-up question found here

1

There are 1 best solutions below

2
On

See, that $5$ and $16$ are relatively prime. So we need to $$\begin{cases}x+2y \equiv 0 \mod 16\\ x-2y \equiv 0 \mod 16\end{cases}$$

We have then: $$x+2y - (x-2y) \equiv 0 \mod 16$$ $$4y \equiv 0 \mod 16$$ So: $$y\in \{16k, 16k+4, 16k+8, 16k+12 | k\in \mathbb{Z}\}$$ Because $x+2y \equiv 0 \mod 16$, we have:

  • for $y=16k$: $x\equiv 0 \mod 16$
  • for $y=16k+4$: $x\equiv 8 \mod 16$
  • for $y=16k+8$: $x\equiv 0 \mod 16$
  • for $y=16k+12$: $x\equiv 8 \mod 16$

The valid pairs $(x,y)$ are then: $$(x,y) \in \{(16l, 16k),(16l+8, 16k+4),(16l, 16k+8),(16l+8,16k+12)|l,k \in \mathbb{Z}\}$$