Given a rational, how to find the integers whose quotient it is?

292 Views Asked by At

I haven't found an answer to this anywhere. Excluding brute force, given a rational $q$ in its decimal form ($1.47$, for example), is there a good algorithm to find integers $m$ and $n$ such that $\frac m n = q$? Thank you.

3

There are 3 best solutions below

2
On BEST ANSWER

If $q$ is given as a decimal, then as $q$ is rational, there are two possibilities:

  1. $q$ has a finite decimal representation, or
  2. $q$ has a repeating decimal representation.

If $q$ has a finite decimal representation, then $10^nq$ is an integer, call it $m$, for some choice of $n$ (in particular, $n$ is the number of digits after the decimal). In that case, $q = \dfrac{m}{n}$.

For example, if $q = 1.234$, $n = 3$ so $m = 10^3q = 1000\times 1.234 = 1234$. Therefore $$q = \dfrac{1234}{1000} = \dfrac{617}{500}.$$

If $q$ has a repeating decimal expansion, write $q$ as $r + s$ where $r$ has finite decimal representation and $s$ is the 'repeating part'.

For example, if $q = 1.234567567567\dots$, then $r = 1.234$ and $s = 0.000567567567\dots$

Using the first part, you can write $r$ as a fraction, so you just need to know how to write $s$ as a fraction. First, set $s' = 10^ts$ where $t$ is the number of zeroes between the decimal point and the number which repeats.

For $s = 0.000567567567\dots$, $t = 3$ so $s' = 10^3s = 0.567567567\dots$

Now let $l$ denote the number which repeats in $s'$, which may have many digits.

In the case $s' = 0.567567567\dots$, $l = 567$.

Now there is some $k$ such that $10^ks$ has integer part $l$; in particular, $k$ is the number of digits in $l$.

When $s' = 0.567567567\dots$, $k = 3$ because $l = 567$ has three digits.

Now note that $10^ks' - l = s'$.

We have $10^3s' - l = 567.567567\dots - 567 = 0.567567\dots = s'$.

Therefore, $(10^k - 1)s' = l$ so $s' = \dfrac{l}{10^k-1}$.

As $10^3s' - l = s'$ we have $s' = \dfrac{1}{10^3 - 1} = \dfrac{567}{999}$.

Once you have $s'$ as a fraction, you can easily determine $s$ as a fraction by using the fact that $s = 10^{-t}s'$. Together with the fraction for $r$, you can then determine the fraction for your original $q$.

As $s' = \dfrac{567}{999}$, we have $s = 10^{-3}s' = \dfrac{567}{999000} = \dfrac{21}{37000}$. As $r = 1.234$, we have $r = \dfrac{617}{500}$.

Therefore $$q = r + s = \frac{21}{37000} + \frac{617}{500} = \frac{45679}{37000}.$$

2
On

If $q$ is in decimal form well yes, there's the whole "if $q$ is not periodic then it is $q$ without . divided by $10^n$, if it's periodic do this and that" (if you meant this comment and I'll write it all)

0
On

Here's the technique I learned in grade school: examine $q$ to determine its finite and repeating parts: $q = a{.}f\bar{r}$, where $a$ is the integer part, and $f$ and $r$ are the finite and repeating parts. Say $f$ has length $k$ and $r$ has length $l$, so that after multiplying $q$ by $10^k$, we get $x = 10^k q = af{.}\bar{r}$, and multiplying that by $10^l$ we get $y = 10^{k + l} q = afr.\bar{r}$. Note that $y - x = afr - af$ as well as $y - x = 10^k(10^l -1)q$. So solving for $q$, we get $q = (afr - af)/ 10^k(10^l -1)$.

Note that $af$ and $afr$ are the concatenations of $a$, $f$, and $r$, not their product; i.e. $af = 10^k a + f$ and $afr = 10^l af + r$.