TITLE: Approximating reals with rationals
QUESTION [8 upvotes]: I don't have any particular motivation for this question, it just popped into my head.
We play the following game: you give me a real number between 0 and 1, and I have to do my best to approximate it as a rational number with a limit on the size of the denominator. More formally, you choose $x \in [0,1]$, and I have to come up with a pair of integers $p,q \leq N$ that minimize the error:
$$
E = \bigg| x - \frac{p}{q} \bigg|
$$
For a given $N$, how should you choose $x$ to make me incur the largest possible error?
I made a plot to see how all the possible fractions fall on the number line for each $N$ between 1 and 100. It has a rather interesting pattern which seems to have a fractal nature. For any $N$, the hardest choices of $x$ seem to be at the extremes of the interval: between 0 and the smallest possible fraction larger than 0, and between 1 and the largest possible fraction smaller than $1$.

REPLY [3 votes]: The ordered set of fractions you can choose is by definition the Farey sequence of order $N$. All Farey sequences have the property that any two adjacent fractions $\frac ab<\frac cd$ are separated by a gap of $\frac1{bd}$. I thus want to find adjacent fractions with $bd$ as small as possible for the given $N$, and then choose the number midway between those two fractions.
Now I argue that except if $b=1$ or $d=1$, two adjacent fractions must have $bd>N$. The natural number inequality $x+y<xy$ holds whenever $x,y>1$ and $(x,y)\ne(2,2)$. If $bd\le N$, the mediant $\frac{a+c}{b+d}$ of the assumed-adjacent fractions would come between them in the Farey sequence of order $N$ – contradiction. For $b=d=2$ there is only one selectable fraction with denominator $2$, so we cannot speak of two such fractions being adjacent in the first place.
Now $b=1$ or $d=1$ means the largest gap I desire lies at the endpoints. Accordingly, to induce the largest approximation error I choose $\frac1{2N}$ or $1-\frac1{2N}$, which incurs the largest error of $\frac1{2N}$.