is a bijection between the set and the set of pairs (remember that the notation means the set ). In other words, if we draw an grid and trace out a diagonal path, wrapping around when we get to an edge, we will hit every grid square exactly once before returning to the start. In other other words, the set of equations , always has a unique solution (that is, unique ) when and are relatively prime.
Let’s prove it! First, let’s prove that the function is one-to-one, that is, any two different values of are sent to different pairs . In other words, if and are not equal, then and will also not be equal. In symbols:
Notice how this is phrased negatively, in terms of things being not equal to each other. It is usually much easier to prove the contrapositive of this statement, namely
That is, if and map to the same remainders , then and must in fact be the same. This is logically equivalent (you should try to convince yourself of this if you’re unsure!) but much easier to get a handle on.
So, suppose we have numbers and , both non-negative and less than , and suppose they give the same results and , that is, and . Consider their difference . Since and have the same remainder when divided by , it must be the case that is evenly divisible by ; likewise, is evenly divisible by . But since and are relatively prime, that is, they share no common factors, they cannot overlap at all in ’s prime factorization; so in fact must be divisible by their product, . In theory, then, could be one of the values . Which is it? Well, remember that , and . Subtracting two nonnegative integers less than could never produce any multiple of other than . So in fact , which means .
So the function is one-to-one. But it maps between two finite sets of the same size—namely, and , both of which have exactly elements. If a function between two finite sets of the same size is one-to-one, it must actually be a bijection. That is, if we have the same number of things on each side, and a way to send each item on the left to a distinct item on the right, then we in fact have a way to match up every element on both sides with exactly one on the other side.
As a final note, this proof certainly has simplicity going for it, but you may find it a bit unsatisfactory: although it tells us that the function has an inverse, it doesn’t tell us how to compute it. That is, given a particular pair of remainders where and , how do we find ? It can certainly be done. But for now I will let you look it up, and perhaps I’ll write about it at some point in the future!
]]>And then in the next post I explained how I made the images: starting in the upper left corner of a grid, put consecutive numbers along a diagonal line, wrapping around both from bottom to top and right to left. The question is then why some grids get completely filled in, like the one above, and why some have holes, like this:
Commenter ZL got the right answer: the grid will be completely filled in if and only if the width and height of the grid are relatively prime.
In fact, this is (a special case of^{1}) the Chinese remainder theorem (as commenter Jon Awbrey foresaw). In more traditional terms, the Chinese remainder theorem says that if we have a system of two modular equations
then as long as and are relatively prime, there is a unique solution for in the range .
Here’s another way to think about this. Suppose I have a number in the range . If I tell you only , that is, the remainder when dividing by , you can’t tell for sure what is; there may be multiple possibilities for . Likewise, if I only tell you , you don’t know . But if I tell you both—as long as and are relatively prime—it’s enough information to precisely pin down the value of ; there is only one value of which has a certain remainder modulo and at the same time a certain remainder modulo .
Yet another way to state the theorem is to consider the function
which sends an integer to a pair of integers where the first is in the range and the second is in the range . If we use the notation then we can write this concisely as
The Chinese Remainder Theorem just says that this function is actually a bijection, that is, it is onto (every pair has some solution such that ) and one-to-one (the for a given is unique).
But this function is exactly what the grid diagrams are visualizing! We start at in the upper left, and every time we step to the next value of , we take a step down; so in general the number will be on row (if we number the rows starting with row at the top)—except that we wrap around when we get to the bottom. So if there are rows, that means will be on row . Similarly, we take a step to the right with each next value of , and wrap around; so will be in column . Hence each will end up at grid coordinates , which is exactly the function . And being a bijection is exactly the same thing as the grid being completely filled in: every value of corresponds to a different grid square, and every grid square has a value of .
In my next post I’ll go over a simple proof of the theorem.
The full Chinese Remainder Theorem actually says something about the situation where you have an arbitrary number of these equations, not just two; but the version with only two equations has all the essential features. (Indeed, once you believe the version with two equations, you can “bootstrap” your way to dealing with an arbitrary number just by applying the two-equation version repeatedly.)
By contrast, it is very easy to decide whether it is possible to write 33 as a sum of two squares, . Since squares can only be positive, any values of and greater than are not going to work, since they would make the sum too big. So there are only a few pairs of values to check: it’s enough to just check all pairs with (quick: how many such pairs are there?), which can even be done by hand in a few minutes. None of them work, so this exhaustive search of all the possibilities proves that it is not possible to find and such that .
But a sum of three cubes, , is an entirely different matter! We can’t put any a priori bound on the size of the values , , and , because the cube of a negative number is negative—if we choose at least one of them to be positive and at least one of them to be negative, they could in theory be very large but “cancel out” to yield 33. And that’s exactly what Dr. Booker found (using approximately 23 years’ worth of computer time spread over one month!):
Whoa.
]]>What’s going on? Well, first, it’s easy to notice that each grid starts with in the upper-left square; is one square down and to the right of , then is one square down and to the right of , and so on….
When we get to an edge, we “wrap around” from the bottom to the top and/or from the right to the left. For example, in the grid above, notice how the is still one space to the right of the , but in the top row; likewise, the is below the but in the left column; then the immediately wraps back around to the top row, and so on.
In other words, we can imagine that we are really making a straight diagonal line, but the bottom edge of the grid is “glued” to the top edge, and the right edge is glued to the left edge (making a torus, aka donut).
Equivalently, we can imagine gluing a bunch of copies of the grid together along their edges, and the numbers just count in a straight diagonal line, moving from one grid to the next, like this:
Although really, since each grid is supposed to be a copy, we should draw a diagonal sequence of numbers starting at in the upper-left corner of every copy!
So in this particular example, if we keep doing this, we eventually fill up the entire grid/space:
But sometimes that doesn’t happen; for example:
In this example, when we get to the in the bottom-right corner, we would have to wrap around to the top left again, but the is already there. So the process stops before we actually fill up the whole grid. This is why some of the grids have empty spaces in them. Here’s another slightly more interesting example:
So, what’s the difference? How can you tell whether a particular grid is going to fill up or have empty spaces left over?
]]>But then Goldilogs noticed that the chairs were arranged in order, from smallest to largest, and she clapped her hands with delight. “What organized people must live here!” she exclaimed. “I wonder, how ever did they arrange their chairs this way?” So she tried the chair in the middle of the row, and it was too big. Next she tried the chair a quarter of the way along, and it was too small. She continued in this manner, eliminating half of the remaining chairs with each test, until finally she found one that was Just Right. In fact, she found it so quickly that she had plenty of time to sit and enjoy reading from the selection of algorithms textbooks that she found on a shelf, and then she put everything back the way she found it and left before the bears got home.
]]>What about ? This is a bit tricker to do efficiently. Let denote the result after removing all factors of or from , as before. Then we want to be evenly divisible by , or put another way, we want the smallest such that
So the obvious method is to compute , , , and so on, and find the first which yields . Practically speaking, we don’t have to compute from scratch at each step: we can start with , and then at each step multiply the current value by and reduce . We keep doing this until we reach again, and record how many steps it took. For example,
This took 6 steps, therefore is the smallest such that is divisible by 21; this matches what we saw in the previous posts, for example, the fact that has a repetend length of (remember that removing factors of 2 and 5 from yields ).
This method is correct, but we can do better—although for this we’ll need a little bit of group theory!
We can use this knowledge to find more quickly than simply counting powers of : just list all the divisors of , try each one by raising to the power of the divisor (using fast modular exponentiation of course), and pick the smallest one that yields . Using our example from before of , we find that , and so has to be one of , , , , , or . As another, bigger example, suppose . Then we can compute
.
(I was going to link to an explanation of how to compute —but it seems although I’ve mentioned 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 divisors we would have to try (since to make a divisor of , there are choices for the exponent of (namely, 0 through 5), 6 choices for the exponent of 3, and 2 choices for the exponent of ). things to try is much, much better than trying every single possible value from up to ! I wrote a little computer program to try them all, and it turns out that is the smallest divisor that works. Hence, we find that has a repetend of length (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!
]]>What are the smallest values of and such that is evenly divisible by ?
and I left it at that, with some questions for thought.
Can you see why we get to determine the values of and separately, i.e. neither value influences the other at all?
The reason we get to consider and separately is that and cannot possibly share any prime factors in common. only has 2 and 5 as prime factors; on the other hand, cannot have 2 or 5 as prime factors since it is equivalent to and . So the original question splits into two independent questions: (1) What is the smallest value of such that cancels all the factors of and in ? (2) What is the smallest value of such that cancels all the other prime factors in ?
The first sub-question is easy enough to answer: if , where has no factors of or , then choosing is both necessary and sufficient: it will be just enough to cancel all the factors of and in .
Let’s see how this works. The example we looked at last time was . If we factor we get , so our analysis above says , since there are two factors of . And sure enough, the decimal expansion has a prefix of length .
As another example, let’s pick a denominator by its factorization: suppose we have a denominator of . 5 has a higher exponent than the 2, so we predict the prefix will have length 4: and sure enough, for example,
We can try other numerators too, we just have to make sure they are relatively prime to . For example,
.
In my next post I’ll talk about how to find the repetend length!
]]>(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.
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 .
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:
(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.)
]]>Theorem: if is composite and there exists at least one Fermat witness for , then at least half of the numbers relatively prime to are Fermat witnesses.
Were you able to prove this? Here’s one way to do it.
Suppose is composite and is a Fermat witness for —that is, . Let be some Fermat liar for , that is, is relatively prime to but . (How do we know there exists a Fermat liar for , you ask? Well, we don’t, but if there aren’t any then the theorem is obviously true.) Then I claim that is also a Fermat witness for :
So for every Fermat liar there is a corresponding Fermat witness . The only thing we might worry about is that some of these Fermat witnesses might not be distinct. But in fact, since is relatively prime to , multiplication by modulo is invertible, so and must be distinct modulo whenever and 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 , each we pick has a probability of at least of revealing that is composite. So if we pick different values for and all of them yield , we can conclude that there is only a chance that is composite. That’s pretty good, and it’s nice that the probability is independent of .
However, there’s one big flaw, which relates to my third question from the previous post: can there be composite values of with no Fermat witnesses? More in my next post!
]]>