# How to tell / quantify how much a number is close to some simple integer ratio?

It's easy for us to tell that 0.49999 is only 0.00001 away from being expressed as a simple ratio: 1/2.

However, it may not be as obvious that 0.142858 is also at most only 0.00001 away from being expressed as a simple ratio. 1/7 in this case.

For our purpose a simple ratio will be defined as a fraction where both the numerator and the denominator consist of a single digit.

Is there a way to calculate the closest simple ratio to a number other than comparing the difference between every ratio and the number in question?

How would you generalize this to approximating simple ratios using integers up to n for the numerator and denominator?

An algorithm for finding the best rational approximation with a given range of denominator in Wikipedia. Basically you find the continued fraction expansion of the number, then massage the last entry a bit. If you ask Alpha for the continued fraction for $$0.142858$$ you get $$[0; 6, 1, 23808, 1, 2]$$ which represents $$0+\frac 1{6+\frac 1{1+\frac 1{23808+\ldots}}}$$ That huge entry $$23808$$ says that stopping just before it gives a very good approximation, which here is $$\frac 17$$

• This did the trick. Many thanks! May 12 '20 at 14:18
• I added the link to what I fed Alpha. You can also ask for the convergents directly with wolframalpha.com/input/?i=convergents+0.142858 May 12 '20 at 14:51

However, it may not be as obvious that 0.142858 is also only 0.00001 away from being expressed as a simple ratio. 1/7 in this case.

Not only may it not be obvious, but it also is not true either :).
0.142858 is only 0.00001 from 0.142857 (or 0.142859) which is 142857/1000000 (or 142857/1000000). Neither is equal to 1/7 (nor are they simple fractions).

• I think they meant "at most $0.00001$ away" May 12 '20 at 13:19
• You should Google "continued fractions". May 12 '20 at 13:20
• @saulspatz this seems like exactly what I needed! Much thanks! May 12 '20 at 13:24
• @Aryaman Maithani, thanks for pointing it out. I corrected the typo. May 12 '20 at 13:25

I'll answer the more general question. Your first question is just the case $$n=9$$.

Let $$x$$ be a real number. Then $$x$$ can be written as a simple ratio if, and only if, there exists $$b\in\{1,\ldots,n\}$$ such that writing $$x$$ in base $$b$$ has at most one digit after the dot. This is because "has at most one digit after the dot" is equivalent to "is a multiple of $$\frac 1b$$".

Checking that a number is close to a simple ratio is then a matter of checking if there are a lot of $$0$$ or $$b-1$$ at the end of its expression in base $$b$$.