Find non-repeating decimal between two fractions

1.2k Views Asked by At

I have two fractions which, when evaluated, may or may not be non-terminating (they go on forever). They're both between 1 and 0.

I need to find the shortest decimal number that lies between these fractions which terminates.

Anyone have an idea? I'm a simple programmer not a mathematician so try not make it too complicated if possible.

Thanks!

2

There are 2 best solutions below

1
On BEST ANSWER

Let the two numbers be $\dfrac{a}b$ and $\dfrac{c}d$. Find the difference, i.e., $$\dfrac{p}q = \dfrac{\vert ad-bc\vert}{bd}$$ Find $m=\lfloor \log(q/p) \rfloor$. This gives the digit where the difference occurs. Now take the largest of the fraction. Find the first $m$ digits, this gives you the answer.

Example:

For instance, consider the fractions, $a/b=37/87$ and $c/d=195/459$. It is easy to check which is lesser. Compare $459 \times 37$ and $87 \times 195$. In this case, $195/459$ is lesser than $37/87$. Compute $$\dfrac{p}q = \dfrac{a}b - \dfrac{c}d = \dfrac2{4437}$$ Now we get $m = \lfloor \log_{10}(4437/2) \rfloor = 3$. Now compute $a/b$ till the third digit. We get $a/b \approx 0.4252\ldots$. Stop at the third digit, which gives us $0.425$. We have $$0.4248 \approx \dfrac{195}{459} < 0.425 \leq \dfrac{37}{87} \approx 0.4252$$

0
On

You can convert them both to decimals by long division. Suppose they are \begin{align} & 0.2308491\,3\,\overline{476}\,476476476\ldots \\ & 0.2308491\,\overline{3}\,333333333333\ldots \end{align} so the first digit where they differ is the ninth digit, where one of them has a $4$ and the other a $3$. One of them has $476$ where the other has $333$. Pick some integer strictly between $333$ and $476$ --- for example $400$. Put that there, getting $$ 0.23084914. $$ That is between the other two, and it terminates. If you need the shortest one, notice that just putting a $4$ there and stopping does it.