…just kidding! I’ll explain what necklaces are in a minute. But first, let me state that we will be proving statement (3) of FlT, namely,

If is prime and is any integer, then .

Subtracting from both sides, this is the same as saying that , which is in turn the same as saying that is evenly divisible by . As we have already seen, this statement is equivalent to the other statements (1) and (2) of Fermat’s Little Theorem.

Now, this is a necklace:

That is, a necklace is a circular arrangement of colored beads. (I’ve also labelled each bead with a number corresponding to its color, to make it easier to distinguish them.) Two necklaces are the same if they are *rotations* of each other. For example, these three necklaces, although they are drawn differently, are all the same:

Unlike in real life, though, we’re not allowed to flip necklaces over,^{1} so these two necklaces are different:

Now, suppose we have different bead colors to choose from. Assume that we have a plentiful supply of each bead color, so we can use as many beads of each color as we want. Here is the central question we will consider:

*How many necklaces are there with exactly beads and at least two different colors?*

We will show that there are in fact such necklaces—and since the *number of* necklaces is obviously an integer, this means that must evenly divide . So, on with the proof!

If you cut a necklace in between two beads and open it up, you get a *strand* (also commonly called a *string* or a *word*). Like this:

Let’s start by counting strands. First, the number of different strands with beads chosen from possible colors is : for each one of the beads we can independently choose any one of the available colors, for a total of choices. For example, if and , here are the different possible strands with beads and (up to) two colors:

Notice that we don’t *have to* use all the colors in each strand. is the number of *available* colors, but just because a color is available doesn’t mean we have to use it. As another example, here are the strands with three beads chosen from among three available colors:

Now, some of these strands use only a single color, that is, all their beads are the same color. In fact, there are exactly such strands, one for each color. So the number of strands with at least two different colors is : the total number of strands minus the strands with only one color.

So, how many *necklaces* are there? Let’s work backwards: think about starting with a necklace and cutting it at various points to create different strands. For example, if we start with the necklace shown below and cut it in all five possible locations, we get five distinct strands:

You can check that these five strands are all different. However, this is not always the case. For example, cutting the necklace shown below yields only three different strands:

I’ve drawn six strands above, representing all six possible cuts, but as you can see, they are not all unique—in fact, there are two copies of each strand.

What’s the difference? Well, the difference is that is prime whereas is not! Remember that we assumed , the length of our necklaces, is prime. And I claim that every necklace *of prime size* , that uses two or more colors, yields *exactly distinct strands when cut*.

Why is that? Think about what it means if a necklace yields the same strand when cut in two different places which are spaced beads apart. (In the example shown above, .) Since the strands are the same, the beads right after each cut must be the same; the beads two places after each cut must be the same, the beads three places after each cut must be the same, and so on—in general, if you look at any two beads spaced apart, they must be the same. But this means that the whole necklace has a *period* of —that is, the necklace consists of a group of beads which is repeated some number of times. But if the whole necklace is made up of some number of repetitions of a group of beads, that means must evenly divide the size of the necklace . But if is prime, the only way for this to happen is if , in which case the necklace consists of a single repeated bead, that is, the necklace only has one color.

So, when is prime, each necklace with at least two bead colors corresponds to exactly different strands—and each strand corresponds to exactly one necklace (the one you get when you glue the ends of the strand together). For example, here is the correspondence between the different necklaces of size with at least two colors chosen out of , and the different strands containing both colors. Each necklace corresponds to exactly different strands.

We already know there are strands with at least two colors; since we can put them in groups of , one for each necklace of at least two colors, must be evenly divisible by . QED!

]]>

If and is any integer relatively prime to , then .

Remember that is the function that tells us how many positive integers less than are relatively prime to (share no common factors with) . So, for example, suppose and . There are four positive integers among which are relatively prime to (namely, , , , and ), so the theorem says we should have , which we can verify: .

Once again, we start by considering the set of multiples of up to :

.

However, in this more general context we don’t want to use all of them! Our proof of Fermat’s Little Theorem relied on the fact that if (here, ) is prime, then none of have any factors in common with it. But if is not prime that may no longer be true. So, instead of the set above, we will look at the set of multiples of *which are relatively prime to *. Since we already assumed is relatively prime to , this is equivalent to saying we want multiples where is relatively prime to :

( is another way to say that and are relatively prime: they share no factors in common if and only if they have no nontrivial common divisor, that is, their greatest common divisor is .) Note that : the number of things relatively prime to between and is exactly what counts.

As an example, suppose and . The only multiples of which are relatively prime to are

If we take these , we get

and hey, look at that—it’s the numbers relatively prime to again, just in a different order! As another example, suppose and . The numbers relatively prime to are , and we can check that

So the claim is that if we start with all the numbers relatively prime to , multiply them all by , and then take their remainders , we get back the same set we started with (though quite possibly in a different order). More formally, let be the set of numbers relatively prime to . Then the claim is that

where multiplying a number by a set, like , means to multiply each element of the set by the given number.

The proof of this claim starts out quite similar to the proof from last time. First, suppose there are two multiples of , say, and , which have the same remainder when divided by . We can write this as . Subtracting from both sides and factoring out , we find that , that is, is divisible by . By assumption, is relatively prime to , so the only way for this to be true is if is divisible by , but since and are less than , the only way for this to happen is if , that is, . So this means that if we start with different multiples of , we get different remainders out.

However, this isn’t enough—this time we actually care about *which* remainders we get. We have to prove that if we start with only those multiples which are relatively prime to , we get exactly the same remainders out. So far, we haven’t used the fact that and are relatively prime to at all. Indeed, if we take *all* multiples of , we still end up with each remainder only once:

However, you’ll see later why we specifically want *only* the remainders which are relatively prime to .

So, assuming is relatively prime to , consider the remainder we get after dividing by . So, is more than some multiple of , that is, . We want to show that is relatively prime to . If had a factor in common with , then the whole right-hand side would be divisible by (because a sum of multiples of is also a multiple of ). But then the left-hand side, , would be divisible by also, but neither nor can be divisible by , since both are relatively prime to . Hence in fact has no factors in common with .

We’re almost done now. We start with the equation

and, just like last time, we now multiply together everything on the left and everything on the right. On the left we can factor out copies of , and this leaves us with

where means to take the product of all the elements in a set. Finally, we can see why it was important that we only took multiples of which are relatively prime to : this means the product shares no common factors with , which means we can divide by it on both sides, concluding that

If we had instead used the multiples of as before, we would end up with

which is a valid equation, but the problem is we are stuck here: dividing both sides by would *not* result in a valid modular equation, since shares one or more common factors with when is not prime.

]]>

Today I want to present the first proof of FlT. We’re going to prove statement (2), that is,

If is a prime and is any integer not divisible by , then .

We already saw that statements (1), (2), and (3) are logically equivalent, so proving (2) is sufficient to prove all of them. (As I’ll show in a future post, we can also generalize this proof to prove the corrected version of statement (4).)

So, suppose is a prime, and is any integer not divisible by . Now consider the set of multiples of up to :

.

However, we will consider not the multiples of themselves but their *remainders* when divided by . As an example, suppose and . Then we want to look at multiples of : —and then take their remainders . As you can check, this yields the set

.

As another example, suppose and . Then the multiples of , considered , are , , , and so on, ultimately yielding the set of remainders (which you can again check):

Have you noticed anything about the previous examples? It looks like every possible remainder (other than ) shows up exactly once, (though obviously not in order). Will this always be true?

The fact that doesn’t show up is no mystery: we specified that is not divisible by , and in that case none of will be divisible by either, so none of them have a remainder of . But why would their remainders all be different?

Suppose there are two multiples of , say, and , which have the same remainder when divided by . We can write this as . Subtracting from both sides and factoring out , we find that , that is, is divisible by . Well, when a prime divides a product of two things, it must divide one or the other (or both). But we already assumed is not divisible by . Hence must evenly divide . But and are both less than , so their difference must lie strictly between and . The only multiple of strictly between and is zero, so , that is, . So the only way to have is if . Put the other way around, we’ve shown that if then and *don’t* have the same remainder . So this proves that all the multiples of from up to have different remainders when divided by .

Finally, since there are exactly multiples of in our set, and possible nonzero remainders , and all the remainders have to be different, we conclude that each remainder shows up exactly once.

So what? Here comes the clever trick: what happens if we take all those multiples of and *multiply* them all together, and then take the remainder ? Since taking remainders commutes with multiplication (that is, ), this is the same as if we first take their remainders and then multiply those. But we already know that the remainders will contain each number from to exactly once—and if we’re multiplying them then the order doesn’t matter. So,

,

that is, the product of all the multiples of has the same remainder as the factorial of when divided by . For example, looking at the example of and again, the product of the multiples of is , whereas ; but both have a remainder of when divided by .

Now, we can factor the copies of out of the left side, and we are left with

Now we just want to cancel from both sides—though we have to be a little careful when dividing both sides of a modular equation. In general it’s only valid when the thing you want to divide by is relatively prime to the modulus (this same issue came up in my previous post). But that is indeed the case here: is not divisible by (since is prime and is the product of a bunch of things that are all smaller than ). So we are justified in dividing both sides by it, and this completes the proof:

.

]]>

If and is any integer, then .

However, **this is wrong**! We can easily find a counterexample. For example, let and . Then , since there are only two numbers less than that are relatively prime to it (namely, and ; each of , , and shares a common factor with ). But then which has a remainder of , *not* , when divided by .

What’s wrong? It turns out I missed a small but important restriction: this is only true *when is relatively prime to *. Note this is stronger than saying isn’t divisible by ; it says they cannot share any common factors at all. For example, is not divisible by , but they share as a common factor, so this theorem does not apply (and indeed, , not ).

For completeness, here’s a corrected statement of the theorem (I have also fixed the previous post):

If and is any integer relatively prime to , then .

So what’s an example where this theorem *does* work? Let’s keep but try instead: now .

In my next post I’ll present a proof of Fermat’s Little Theorem.

]]>

Let’s start with the statement that looks the least general:

If is prime and is an integer where , then .

(Recall that means that and have the same remainder when you divide them by .) For example, is prime, and we can check that for each , if you raise to the th power, you get a number which is one more than a multiple of :

Here’s a second variant of the theorem that looks slightly more general than the first:

If is a prime and is any integer not divisible by , then .

This looks more general because can be *any* integer not divisible by , not just an integer between and . As an example, let . Then .

We can see that (2) is more general than (1), since if then it is certainly the case that is not divisible by . Hence (2) implies (1). But actually, it turns out that (1) implies (2) as well!

Here’s a proof: let’s assume (1) and use it to show (2). In order to show (2), we have to show that whenever is prime and is any integer not divisible by . So let be an arbitrary prime and an arbitrary integer not divisible by . Then by the Euclidean division theorem, we can write in the form , where is the quotient when dividing by , and is the remainder. can’t actually be , since we assumed is not divisible by . Hence , so (1) applies and we can conclude that . But notice that (since is more than a multiple of ), and hence as well.

So although (2) “looks” more general than (1), the two statements are in fact logically equivalent.

Here’s another version which seems to be yet more general, since it drops the restriction that can’t be divisible by :

If is prime and is any integer, then .

Notice, however, that the conclusion is different: rather than .

As an example, let and again. Then , that is, the remainder of when divided by is . As another example, if , then since both are divisible by .

Once again, although this seems more general, it turns out to be equivalent to (1) and (2).

First of all, to see that (2) implies (3), suppose is prime and any integer. If is divisible by , then and clearly . On the other hand, if is not divisible by , then (2) applies and we may conclude that ; multiplying both sides of this equation by yields .

Now, to see that (3) implies (2), let be a prime and any integer not divisible by . Then (3) says that ; we wish to show that . However, since is not divisible by we know that has a multiplicative inverse , that is, there is some such that . (I have written about this fact before; it is a consequence of Bézout’s Identity.) If we take and multiply both sides by , we get to cancel one from each side, yielding as desired.

The final form is the most general yet: it even drops the restriction that be prime.

If and is any integer relatively prime to , then .

[*The first version of this post accidentally omitted the phrase “relatively prime to ” from the above statement, rendering it false; here’s my later post explaining and correcting the error.*]

is the Euler totient function, *i.e.* the number of positive integers less than which are relatively prime to . For example, since there are four positive integers less than which have no factors in common with : namely, , , , and .

We can see that (4) implies (2), since when is prime, (since *every* integer in is relatively prime to ). None of (1), (2), or (3) directly imply (4)—so it *is*, in fact, a bit more general—but we can generalize some of the proofs of these other facts to prove (4).

]]>

Yes, there is a new small human in my house! So I won’t be writing here regularly for the near future, but do hope to still write occasionally as the mood and opportunity strikes.

Recently I realized that I really didn’t know much of anything about fast primality testing algorithms. Of course, I have written about the Lucas-Lehmer test, but that is a special-purpose algorithm for testing primality of numbers with a very special form. So I have learned about a few general-purpose primality tests, including the Rabin-Miller test and the Baille-PSW test. It turns out they are really fascinating, and not as hard to understand as I was expecting. So I may spend some time writing about them here.

As a first step in that direction, here is (one version of) *Fermat’s Little Theorem (FLT)*:

Let be a prime and some positive integer not divisible by . Then that is, is one more than a multiple of .

Have you seen this theorem before? If not, play around with some small examples to see if you believe it and why you think it might be true. If you have seen it before, do you remember a proof? Or can you come up with one? (No peeking!) There are many beautiful proofs; I will write about a few.

]]>

…and this picture of primitive roots I made a year ago:

At first I didn’t see the connection, but Snowball was absolutely right. Once I understood it, I made this little animation to illustrate the connection more clearly:

(Some of the colors flicker a bit; I’m not sure why.)

]]>

I learned from Lucas A. Brown that this is sometimes known as “Euclid’s Orchard”. Imagine that there is a tall, straight tree growing from each grid point other than the origin. If you stand at the origin, then the trees you can see are exactly those at grid points with . This is because if a tree is at for some , then it is blocked from your sight by the tree at : both lie exactly along the line from the origin with slope . But if a tree is at some point with relatively prime coordinates , then it will be the first thing you see when you look along the line with slope exactly .

(…well, actually, all of the above is only really true if we assume the trees are infinitely skinny! Otherwise trees will end up blocking other trees which are almost, but not quite, in line with them. So try not to breathe while standing at the origin, OK? You might knock over some of the infinitely skinny trees.)

Here’s the portion of the grid surrounding the origin, with the lines of sight drawn in along with the trees you can’t see because they are exactly in line with some closer tree. (I’ve made the trees skinny enough so that they don’t accidentally block any other lines of sight—but if we expanded the grid we’d have to make the trees even skinner.)

Now, what about the colors of the dots? Commenter Snowball guessed this correctly: each point is colored according to the number of steps needed for the Euclidean algorithm needed to reach 1. Darker colors correspond to more steps. It is interesting to note that there seems to be (eight symmetric copies of) one particularly dark radial stripe, indicated below:

In fact, the slope of this stripe is exactly ! This corresponds to the fact (first proved by Gabriel Lamé in 1844) *that consecutive Fibonacci numbers are worst-case inputs to the Euclidean algorithm*—that is, it takes more steps for the Euclidean algorithm to compute than for any other inputs of equal or smaller magnitude. Since the ratio of consecutive Fibonacci numbers tends to , the dots with the darkest color relative to their neighbors all lie approximately along the line with slope . What’s interesting to me is that lots of other dots that lie close to this line are also relatively dark. Why does this happen?

]]>

]]>

What’s the 99th digit to the right of the decimal point in the decimal expansion of ?

However, the solution depended on having the clever idea to add . But there are other ways to come to similar conclusions, and in fact this is not the way I originally solved it.

The first thing I did when attacking the problem was to work out some small powers of by hand:

and so on. It quickly becomes clear (if you have not already seen this kind of thing before) that will always be of the form . Let’s define and to be the coefficients of the th power of , that is, . Now the natural question is to wonder what, if anything, can we say about the coefficients and ? Quite a lot, as it turns out!

We can start by working out what happens when we multiply by another copy of :

But by definition, so this means that and . As for base cases, we also know that , so and . From this point it is easy to quickly make a table of some of the values of and :

Each entry in the column is the sum of the and from the previous row; each is the sum of the previous and twice the previous . You might enjoy playing around with these sequences to see if you notice any patterns. It turns out that there is an equivalent way to define the and separately, such that each only depends on previous values of , and likewise each only depends on previous . I’ll explain how to do that next time, but leave it as a challenge for you in the meantime!

]]>