## Finding the repetend length of a decimal expansion

We’re still trying to find the prefix length $k$ and repetend length $n$ of the decimal expansion of a fraction $a/b$, that is, the length of the part before it starts repeating, and the length of the repeating part. In my previous post we found that the prefix length $k$ is fairly simple to find: it’s equal to the maximum number of factors of either 2 or 5 in $b$.

What about $n$? This is a bit tricker to do efficiently. Let $b'$ denote the result after removing all factors of $2$ or $5$ from $b$, as before. Then we want $(10^n - 1)$ to be evenly divisible by $b'$, or put another way, we want the smallest $n$ such that

$10^n \equiv 1 \pmod {b'}.$

So the obvious method is to compute $10 \bmod {b'}$, $10^2 \bmod {b'}$, $10^3 \bmod {b'}$, and so on, and find the first which yields $1$. Practically speaking, we don’t have to compute $10^n$ from scratch at each step: we can start with $1$, and then at each step multiply the current value by $10$ and reduce $\pmod {b'}$. We keep doing this until we reach $1$ again, and record how many steps it took. For example,

$\begin{array}{rcl} 10 &\equiv & 10 \pmod{21} \\ 10 \cdot 10 = 100 &\equiv & 16 \pmod{21} \\ 16 \cdot 10 = 160 &\equiv & 13 \pmod{21} \\ 13 \cdot 10 = 130 &\equiv & 4 \pmod{21} \\ 4 \cdot 10 = 40 &\equiv & 19 \pmod{21} \\ 19 \cdot 10 = 190 &\equiv & 1 \pmod{21} \end{array}$

This took 6 steps, therefore $n = 6$ is the smallest $n$ such that $10^n - 1$ is divisible by 21; this matches what we saw in the previous posts, for example, the fact that $89/420$ has a repetend length of $6$ (remember that removing factors of 2 and 5 from $420$ yields $21$).

This method is correct, but we can do better—although for this we’ll need a little bit of group theory!

• Recall that $\Phi(b')$ is the set of numbers between $1$ and $b'-1$ (inclusive) which share no common factors with $b'$.
• $\Phi(b')$ actually forms a group under multiplication $\pmod {b'}$: multiplying two values of $\Phi(b')$ always yields another such value, and for every $x \in \Phi(b')$ there is some $y \in \Phi(b')$ such that $xy \equiv 1 \pmod{b'}$.
• We are then really asking for the order of the element $10$ in the group $\Phi(b')$.
• By Lagrange’s Theorem, the order of an element must evenly divide the order of the group.
• The order of a group is just its size; in this case the order of $\Phi(b')$ is $\varphi(b')$, the Euler totient function of $b'$.
• Hence we have learned that $n$—the order of $10$ in the group $\Phi(b')$—must evenly divide $\varphi(b')$.

We can use this knowledge to find $n$ more quickly than simply counting powers of $10$: just list all the divisors of $\varphi(b')$, try each one by raising $10$ to the power of the divisor $\pmod {b'}$ (using fast modular exponentiation of course), and pick the smallest one that yields $1$. Using our example from before of $b' = 21$, we find that $\varphi(21) = (3-1)(7-1) = 12$, and so $n$ has to be one of $1$, $2$, $3$, $4$, $6$, or $12$. As another, bigger example, suppose $b' = 3^5 \cdot 7 \cdot 89 = 151389$. Then we can compute

$\varphi(b') = (3-1) \cdot 3^4 \cdot (7-1) \cdot (89-1) = 2^5 \cdot 3^5 \cdot 11 = 85536$.

(I was going to link to an explanation of how to compute $\varphi$—but it seems although I’ve mentioned $\varphi$ many times before, I’ve never actually written about how to compute it! I’ll have to do that soon, since it’s a very interesting topic. In any case, if you don’t know how, you can just take my word for now.) This number has $6 \cdot 6 \cdot 2 = 72$ divisors we would have to try (since to make a divisor of $2^5 \cdot 3^5 \cdot 11$, there are $6$ choices for the exponent of $2$ (namely, 0 through 5), 6 choices for the exponent of 3, and 2 choices for the exponent of $11$). $72$ things to try is much, much better than trying every single possible value from $1$ up to $151388$! I wrote a little computer program to try them all, and it turns out that $n = 1188 = 2^2 \cdot 3^3 \cdot 11^1$ is the smallest divisor that works. Hence, we find that $1/151389$ has a repetend of length $1188$ (but I’m sure you knew that already =).

It turns out we can do this computation even faster—but that will have to wait for another post!

## Finding the prefix length of a decimal expansion

Remember from my previous post that we’re trying to find the prefix length $k$ and repetend length $n$ of the decimal expansion of a fraction $a/b$, that is, the length of the part before it starts repeating, and the length of the repeating part. In that post I showed how to reduce it to the following question:

What are the smallest values of $k$ and $n$ such that $10^k \cdot (10^n - 1)$ is evenly divisible by $b$?

and I left it at that, with some questions for thought.

• Can you see why we get to determine the values of $k$ and $n$ separately, i.e. neither value influences the other at all?

The reason we get to consider $k$ and $n$ separately is that $10^k$ and $(10^n - 1)$ cannot possibly share any prime factors in common. $10^k = 2^k 5^k$ only has 2 and 5 as prime factors; on the other hand, $(10^n - 1)$ cannot have 2 or 5 as prime factors since it is equivalent to $(-1) \bmod 2$ and $\bmod 5$. So the original question splits into two independent questions: (1) What is the smallest value of $k$ such that $10^k$ cancels all the factors of $2$ and $5$ in $b$? (2) What is the smallest value of $n$ such that $(10^n - 1)$ cancels all the other prime factors in $b$?

The first sub-question is easy enough to answer: if $b = 2^x 5^y b'$, where $b'$ has no factors of $2$ or $5$, then choosing $k = \max(x, y)$ is both necessary and sufficient: it will be just enough to cancel all the factors of $2$ and $5$ in $b$.

Let’s see how this works. The example we looked at last time was $\displaystyle 89/420 = 0.21\overline{190476}$. If we factor $420$ we get $(2^2 \cdot 5) \cdot (3 \cdot 7)$, so our analysis above says $k = 2$, since there are two factors of $2$. And sure enough, the decimal expansion has a prefix of length $2$.

As another example, let’s pick a denominator by its factorization: suppose we have a denominator of $2^2 \cdot 5^4 \cdot 11 \cdot 19 \cdot 37 = 19332500$. 5 has a higher exponent than the 2, so we predict the prefix will have length 4: and sure enough, for example,

$\displaystyle 1/19332500 = 0.0000\overline{000517263675158412}.$

We can try other numerators too, we just have to make sure they are relatively prime to $19332500$. For example,

$\displaystyle 1500007 / 19332500 = 0.0775\overline{899133583344109659}$.

In my next post I’ll talk about how to find the repetend length!

Posted in number theory, pattern | Tagged , , , , | 3 Comments

## Finding prefix and repetend length

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 $a/b$ 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:

$a/b = 0.p_1 p_2 \dots p_k \overline{r_1 r_2 \dots r_n}$

(Let’s assume $a < b$; if $a > b$ then we can just pull out the integer part before analyzing the part after the decimal point.) We’ll call $p_1 \dots p_k$ the prefix, which has $k$ digits, and $r_1 \dots r_n$ is the $n$-digit repetend.

The question is, given a fraction $a/b$, how long will the prefix and repetend be (i.e. what are the smallest possible values of $k$ and $n$)? In particular, it possible to compute $k$ and $n$ 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

$\displaystyle \frac{89}{420} = 0.21\overline{190476}$

It starts with a two-digit prefix $21$ (that is, $k = 2$), and after that it repeats the sequence of $n = 6$ digits $190476$ forever.

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

$\displaystyle 10^2 \cdot \frac{89}{420} = 21.\overline{190476}$

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

$\displaystyle 10^2 \cdot 10^6 \cdot \frac{89}{420} = 21190476.\overline{190476}$

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:

$\displaystyle 10^2 \cdot 10^6 \cdot \frac{89}{420} - 10^2 \cdot \frac{89}{420} = 21190476.\overline{190476} - 21.\overline{190476} = \text{an integer}.$

(Of course in this case the integer is $21190476 - 21 = 21190455$, 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

$\displaystyle 10^2 \cdot (10^6 - 1) \cdot \frac{89}{420} = \text{an integer}.$

Let’s think about why this is an integer. Note that $420 = 10 \cdot 2 \cdot 21 = 2^2 \cdot 3 \cdot 5 \cdot 7$. First, the $10^2 = 2^2 \cdot 5^2$ cancels with the $2^2$ and $5$ in $420$, leaving

$\displaystyle 5 \cdot (10^6 - 1) \cdot \frac{89}{21}.$

Now note that $10^6 - 1 = 999999 = 3^3 \cdot 7 \cdot 11 \cdot 13 \cdot 37$ is divisible by $21$, so the remaining denominator cancels and we are left with an integer. In fact, as you can check, $6$ is the smallest value of $n$ such that $10^n - 1$ is divisible by $21$.

## Generalizing

Let’s generalize: suppose we have

$\displaystyle \frac{a}{b} = 0.p_1 \dots p_k \overline{r_1 \dots r_n}$

and assume that $a/b$ is in lowest terms, that is, $a$ and $b$ share no common factors. Multiplying by $10^k$ shifts the prefix to the left of the decimal point,

$\displaystyle 10^k \cdot \frac{a}{b} = p_1 \dots p_k.\overline{r_1 \dots r_n},$

then multiplying by $10^n$ and subtracting cancels the repeating part to the right of the decimal point:

$\displaystyle 10^k \cdot 10^n \cdot \frac{a}{b} - 10^k \cdot \frac{a}{b} = \text{an integer}.$

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

$\displaystyle 10^k \cdot (10^n - 1) \cdot \frac{a}{b} = \text{an integer}.$

We want to find the smallest values of $k$ and $n$ which make this work. Note that since we assumed $a$ and $b$ share no common factors, $a$ is actually irrelevant: it doesn’t contribute to cancelling $b$ 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 $k$ and $n$ such that $10^k \cdot (10^n - 1)$ is evenly divisible by $b$?

Or, put another way, how can we choose $k$ and $n$ in such a way that every prime factor of $b$ also shows up in $10^k \cdot (10^n - 1)$? 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 $k$ and $n$ separately, i.e. neither value influences the other at all? (Hint: which prime factors of $b$ could possibly be canceled by $10^k$? Which could be canceled by $10^n - 1$?)
• Can you figure out how to find the right value of $k$?
• Can you say anything at all about the right value of $n$?

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

Posted in number theory, pattern | Tagged , , , | 2 Comments

## More on Fermat witnesses and liars

In my previous post I stated, without proof, the following theorem:

Theorem: if $n$ is composite and there exists at least one Fermat witness for $n$, then at least half of the numbers relatively prime to $n$ are Fermat witnesses.

Were you able to prove this? Here’s one way to do it.

Suppose $n$ is composite and $a$ is a Fermat witness for $n$—that is, $a^{n-1} \not\equiv 1 \pmod n$. Let $b$ be some Fermat liar for $n$, that is, $b$ is relatively prime to $n$ but $b^{n-1} \equiv 1 \pmod n$. (How do we know there exists a Fermat liar for $n$, you ask? Well, we don’t, but if there aren’t any then the theorem is obviously true.) Then I claim that $ab$ is also a Fermat witness for $n$:

$(ab)^{n-1} = a^{n-1} b^{n-1} \equiv a^{n-1} \cdot 1 \not \equiv 1 \pmod n$

So for every Fermat liar $b$ there is a corresponding Fermat witness $ab$. The only thing we might worry about is that some of these Fermat witnesses might not be distinct. But in fact, since $a$ is relatively prime to $n$, multiplication by $a$ modulo $n$ is invertible, so $ab_1$ and $ab_2$ must be distinct modulo $n$ whenever $b_1$ and $b_2$ are. This proves that there are at least as many Fermat witnesses as there are liars; hence at least half are witnesses.

This means that, as long as there is at least one Fermat witness for $n$, each $a$ we pick has a probability of at least $1/2$ of revealing that $n$ is composite. So if we pick $k$ different values for $a$ and all of them yield $a^{n-1} \equiv 1 \pmod n$, we can conclude that there is only a $1/2^k$ chance that $a$ is composite. That’s pretty good, and it’s nice that the probability is independent of $n$.

However, there’s one big flaw, which relates to my third question from the previous post: can there be composite values of $n$ with no Fermat witnesses? More in my next post!

Posted in computation, number theory, primes | Tagged , , , , , | Leave a comment

## Fermat witnesses and liars (some words on PWW #24)

Let $n$ be a positive integer we want to test for primality, and suppose $a$ is some other positive integer with $a < n$. There are then four possibilities:

• $a$ and $n$ could share a common factor. In this case we can find the common factor using the Euclidean Algorithm, and $n$ is definitely not prime. (See my previous post.)

• On the other hand, $a$ and $n$ might not share a prime factor, that is, they might be relatively prime (or put yet another way, their GCD might be $1$). This case breaks down into three subcases:

• $a^{n-1} \not \equiv 1 \pmod n$. In this case we know by (the contrapositive of) Fermat’s Little Theorem that $n$ is definitely composite (although we don’t learn anything about its factorization); $a$ is called a Fermat witness for $n$.
• $n$ is prime. In this case, Fermat’s Little Theorem tells us that $a^{n-1}$ must be equivalent to $1$ modulo $n$. However, computing $a^{n-1}$ doesn’t necessarily tell us much, because the next case is also possible:
• $a^{n-1} \equiv 1 \pmod n$ but $n$ is composite. In this case $a$ is called a Fermat liar for $n$.

The question becomes: for a given composite $n$, how many Fermat liars and Fermat witnesses could there be? How many values of $a$ do we have to test to be “reasonably sure” whether $a$ is prime or composite?

Each pixel in the image from my previous post represents a particular $(a,n)$ pair. Each row represents a value of $n$, with the top row corresponding to $n = 2$ and the bottom row to $n = 600$. Each column represents a value of $a$. Of course we only consider pairs $(a,n)$ with $a \leq n$, which explains the triangular shape. (I include the case $a = n$ to complete the triangle, although these are not interesting from a primality testing point of view.)

The four cases outlined above correspond to the four colors:

• If $a$ and $n$ share a common factor, the pixel is colored yellow.
• If $a$ is a Fermat witness for $n$, the pixel is green.
• If $n$ is prime, the pixel is blue.
• If $a$ is a Fermat liar for $n$, the pixel is red.

Here’s a much smaller sample of the same visualization so we can see more clearly what’s going on.

Primes of course yield blue stripes. Non-primes are stripes of yellow, green, and sometimes red. Testing a particular $n$ to see whether it is prime corresponds to picking random squares from its row: if we hit a yellow or green square, we learn immediately that $n$ is composite. If we hit a red square or a blue square, we aren’t sure. So far, however, things don’t look too bad: there are only a few isolated red squares, so picking two or three random values should be enough to ensure that we either find out $n$ is composite, or can safely assume that it is prime. Let’s continue the picture a bit farther; here are the first 100 values of $n$:

I’ve also included some purple bars to the left of each row, showing what proportion of the numbers relatively prime to $n$ are Fermat liars. So far, none of the purple bars extend beyond the halfway mark (the first vertical line corresponds to half, and the second, slightly darker vertical line corresponds to $1$). It turns out there’s a good reason for this:

Theorem: if $n$ is composite and there exists at least one Fermat witness for $n$, then at least half of the numbers relatively prime to $n$ are Fermat witnesses.

Some questions for you:

1. Can you prove this? The proof is not hard given the right idea.
2. Suppose that for every composite $n$, at least half the relatively prime values of $a$ are Fermat witnesses. If we pick $k$ random values of $a < n$ and find that $a^{n-1} \equiv 1 \pmod n$ for all of them, what can we say about the probability that $n$ is prime?
3. The theorem has that pesky condition, “if there exists at least one Fermat witness for $n$”… do you think there could be composite values of $n$ with no Fermat witnesses? (Hint: go back and look at the picture from my previous post…)
| Tagged , , , , | 1 Comment

## Post without words #24

Image | Posted on by | Tagged , , , | 5 Comments

## The Fermat primality test and the GCD test

In my previous post we proved that if $a$ shares a nontrivial common factor with $n$, then $a^{n-1} \not\equiv 1 \pmod n$, and this in turn proves that $n$ is not prime (by Fermat’s Little Theorem).

But wait a minute, this is silly: if $a$ shares a common factor with $n$, then we don’t need anything as complex as Fermat’s Little Theorem to figure out that $n$ is not prime! All we have to do is compute $\gcd(a,n)$ using the Euclidean Algorithm, and when we find out that the result isn’t $1$, we can immediately conclude that $n$ isn’t prime since it has a nontrivial divisor.

So for comparison, let’s consider this (terrible!) primality test, call it the GCD test: given an $n$ we want to test, pick values of $a$ such that $1 < a < n$ and compute $\gcd(a,n)$ for each one. If we find an $a$ for which $\gcd(a,n) \neq 1$, then $n$ is not prime.

This is terrible for several reasons. First, if we want this test to tell us for certain when $n$ is prime, we essentially have to try all the possible values of $a$. We can optimize a bit by only trying $1 < a \leq \sqrt{n}$—if $n$ has any nontrivial divisors we will be sure to find some that are under $\sqrt{n}$—but this doesn’t help all that much in the grand scheme of things. In fact, this GCD test is actually almost the same thing as trial division, where we try a bunch of different numbers to see whether they evenly divide $n$. Both are essentially trying to find divisors of $n$ by brute-force search.

So suppose instead that we are willing to live with some uncertainty, and we just try some fixed number of values for $a$, and either conclude with certainty that $n$ is composite (if we happen to find an $a$ that shares a nontrivial factor with $n$), or report that it is probably prime. How bad can this be—or put another way, how many values of $a$ do we have to try so we can be “reasonably certain” that $n$ really is prime when the test says it is?

The answer is that it can be very bad indeed. Euler’s totient function $\varphi(n)$ counts the number of integers $\leq n$ which are relatively prime to $n$. So if $n$ is composite and we pick $1 < a < n$ uniformly at random, there are $\varphi(n)$ choices which won’t reveal the fact that $n$ is composite, and approximately $n - \varphi(n)$ choices which do share a common factor with $n$ and hence do reveal the fact that it is composite.

So the question is, how big can $\varphi(n)$ be, relative to $n$? We would like it to be small—which would leave us with many opportunities to learn that $n$ is composite—but in fact it can be quite big. For example, $\varphi(961) = \varphi(31^2) = 31\cdot 30 = 930$, which is not much smaller than $961$ itself. This means that $930$ of the numbers less than $961$ share no factors in common with $961$; only $31$ of them share a factor. In fact, $\varphi(n)$ is big precisely when $n$ has just a few large prime factors, which intuitively is exactly when $n$ is most difficult to factor. For such $n$ there really is no acceptable number of $a$’s we can test with the GCD test in order to be reasonably sure that $n$ is prime—in the case of $961$, for example, each $a$ we randomly pick has only a $31 / 961 \approx 3\%$ chance of sharing a common factor with $n$; and this can get arbitrarily bad as $n$ gets larger.

So remember we started down this path by showing that if we use the Fermat primality test, values of $a$ which share a common factor with $n$ will definitely reveal the compositeness of $n$. But now we know that if we rely on only such values of $a$, not only do we have a very small chance of discovering composite $n$—no better than just using trial division—but more than that, actually using the Fermat test itself would be silly, since we should just use the simpler GCD test instead!

So if the Fermat primality test is worthwhile at all, it must be because there are other values of $a$, which don’t share any common factors with $n$, but nonetheless still reveal the fact that $n$ is composite since $a^{n-1} \not\equiv 1 \pmod n$. So how many of those values of $a$ are there? Stay tuned!

Posted in computation, number theory, primes | Tagged , , , | 1 Comment