We interrupt your regularly scheduled primality testing to bring you something else fun I’ve been thinking about. It’s well-known that any rational number has a decimal expansion that either terminates, or is eventually periodic—that is, the digits after the decimal point consist of some initial sequence of digits, followed by a sequence of digits that repeats forever:

(Let’s assume ; if then we can just pull out the integer part before analyzing the part after the decimal point.) We’ll call the *prefix*, which has digits, and is the -digit *repetend*.

The question is, given a fraction , how long will the prefix and repetend be (*i.e.* what are the smallest possible values of and )? In particular, it possible to compute and without simply doing long division?

It turns out that it *is* possible, and I’d like to explain how it can be done. I wrote about this a long time ago (exactly ten years and two days ago, actually!!) but I think I can do a better job explaining it now.

## An example

Let’s start by looking at the fraction

It starts with a two-digit prefix (that is, ), and after that it repeats the sequence of digits forever.

Let’s play with this a bit. First of all, let’s multiply by to shift the prefix over to the left of the decimal point:

Now if we multiply by another , we shift one entire copy of the repetend to the left of the decimal point as well:

If we subtract these, the infinite repeating parts to the right of the decimal point will cancel, and we will be left with an integer:

(Of course in this case the integer is , but we don’t really care: since we’re only interested in the *length* of the prefix and repetend, it turns out that it doesn’t actually matter what the integer is, only the fact that it is one!)

We can now factor the left-hand side a bit to get

Let’s think about why this is an integer. Note that . First, the cancels with the and in , leaving

Now note that is divisible by , so the remaining denominator cancels and we are left with an integer. In fact, as you can check, is the *smallest* value of such that is divisible by .

## Generalizing

Let’s generalize: suppose we have

and assume that is in lowest terms, that is, and share no common factors. Multiplying by shifts the prefix to the left of the decimal point,

then multiplying by and subtracting cancels the repeating part to the right of the decimal point:

Again, we can factor the left-hand side, yielding

We want to find the smallest values of and which make this work. Note that since we assumed and share no common factors, is actually irrelevant: it doesn’t contribute to cancelling at all. (This explains why repetends are always the same length for a given denominator.) So in the end, the question becomes:

What are the smallest values of and such that is evenly divisible by ?

Or, put another way, how can we choose and in such a way that every prime factor of also shows up in ? I’ll continue in future posts; for now, I leave you with a few questions:

- Can you see why we get to determine the values of and
*separately*,*i.e.*neither value influences the other at all? (*Hint*: which prime factors of could possibly be canceled by ? Which could be canceled by ?) - Can you figure out how to find the right value of ?
- Can you say anything at all about the right value of ?

(*Hints*: finding turns out to be much easier than finding , and requires only knowing some basic facts about divisibility. Saying useful things about requires knowing some number theory and/or group theory.)

Pingback: Finding the prefix length of a decimal expansion | The Math Less Traveled

Pingback: Finding the repetend length of a decimal expansion | The Math Less Traveled