1. Home
  2. Explore
  3. Mathematics
  4. Number Theory and Cryptography

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Number Theory and Cryptography

Transcript

Two whole numbers, and one simple question. Does the first one divide the second one exactly? That small question grows into the mathematics This chapter is about whole numbers, and about the secret codes we build from them. Let's get started. Hello, and welcome to Chapter four. In this chapter we study number theory and cryptography. Number theory is the math of whole numbers. Cryptography is the art of secret codes. We start small, with when one number divides another. Then we build up, step by step, to primes, to greatest common divisors, and at the end, to the math behind secret messages on the internet. Let's begin.

Let's see why this chapter matters. Number theory is the part of math that studies the integers. Integers are the whole numbers, like minus two, zero, one, and one hundred. We look at their properties. One big idea is divisibility. That means, does one number divide another with no remainder? Another big idea is which numbers are prime. We also learn to write numbers in other bases, like binary, which is base two, and hexadecimal, which is base sixteen.

People have loved number theory for a long time. The ideas are beautiful. They are easy to start. And many questions are still open, still unsolved. We will reuse the proof methods from Chapter one. For a long time people thought number theory was pure math, with no use. But today it sits at the heart of computer science and cryptography. We cover those in Sections four point five and four point six.

This is the plan for the whole chapter. Six topics, and each one builds on the one before it. First, divisibility and modular arithmetic. Second, how to represent integers in different bases, and the algorithms for that. Third, primes and greatest common divisors. Fourth, solving congruences. Fifth, applications of congruences. And sixth, cryptography. So let's take them in order.

Section four point one. Divisibility and modular arithmetic. In this section we learn what it means for one integer to divide another. We meet the division algorithm, which gives a quotient and a remainder. And we learn modular arithmetic, which is arithmetic that wraps around, like the hours on a clock. This section has three parts. First, division, and what it means for one number to divide another. Second, the division algorithm, which is really a theorem about quotients and remainders. And third, modular arithmetic, which is arithmetic with remainders.

Let's define division. Take two integers, a and b, and say a is not zero. We say a divides b if we can find an integer c so that b equals a times c. In plain words, b is a whole number of copies of a. When this happens, we call a a factor, or a divisor, of b. And we call b a multiple of a. We write it with a vertical bar. The bar means a divides b. If a divides b, then b over a is a whole number, with no remainder. If a does not divide b, we put a slash through the bar.

Let's work this one step at a time. We ask whether three divides seven, and whether three divides twelve. Step one. Set it up. The number doing the dividing is three. So we ask: is there a whole number c with b equal to three times c? Step two. Try b equal to seven. Seven divided by three is two point three three. Step three. That is not a whole number. So no c works. Three does not divide seven. Step four. Now try b equal to twelve. Twelve divided by three is four. Step five.

Four is a whole number. So twelve equals three times four, and three divides twelve. Now some rules about divisibility. Theorem one. Let a, b, and c be integers, with a not zero. Part one. If a divides b and a divides c, then a divides b plus c. Part two. If a divides b, then a divides b times c, for any integer c. Part three. If a divides b and b divides c, then a divides c. The book leaves parts two and three as exercises. We will prove part one together on the next slide.

First, set it up. We are given that a divides b, and a divides c. By the definition, that means we can write b as a times s, and c as a times t, where s and t are whole numbers. What we must show is that a divides b plus c. Now the working. Line zero is the number we care about: b plus c. Step one. Put in what we know. b is a s, and c is a t. So the sum is a s plus a t. Step two. Both terms have an a in them. Factor it out. We get a times, in brackets, s plus t. Step three.

s plus t is a whole number. So b plus c is a whole number of copies of a. That is exactly what a divides b plus c means. Theorem one has a useful corollary. If a divides b and a divides c, then a also divides m times b, plus n times c, for any whole numbers m and n. A sum like m times b plus n times c is called a linear combination of b and c. So the short version is this. A number that divides both b and c divides every linear combination of them. We will lean on this idea many times in this chapter.

When you divide one whole number by a positive whole number, you get a quotient and a remainder. People call it the division algorithm, but really it is a theorem. Take an integer a, and a positive integer d. Then there are unique integers q and r, where r is at least zero and less than d, so that a equals d times q, plus r. Notice the word unique. There is only one pair that works. And notice the range on r. The remainder is never negative, and never as big as d.

Let's name the four parts of that equation, one at a time. d is the divisor. It is the number we divide by. a is the dividend. It is the number being divided. q is the quotient. We write it as a div d. r is the remainder. We write it as a mod d. So div gives you the whole part, and mod gives you what is left over. Both are functions. You feed them two numbers and they hand back one.

Let's work a division, step by step. We divide one hundred one by eleven. Step one. Set it up. The dividend a is one hundred one. The divisor d is eleven. Step two. Ask how many whole elevens fit inside one hundred one. Eleven times nine is ninety nine. Ten elevens would be one hundred ten, which is too big. So nine is the answer. Step three. Subtract. One hundred one minus ninety nine leaves two. Step four. Check the remainder is in range. It must be at least zero and less than eleven.

Two passes. Step five. Read it off. The quotient, one hundred one div eleven, is nine. The remainder, one hundred one mod eleven, is two. Negative numbers need care, so let's go slowly. We divide minus eleven by three. Step one. Set it up. The dividend a is minus eleven. The divisor d is three. Step two. Now the mistake people often make. Minus eleven equals three times minus three, minus two. That gives a remainder of minus two. But the rule says the remainder must be zero or more. So this is not allowed. Step three. Take the quotient one step further down. Three times minus four is minus twelve. Step four. Now subtract.

Minus eleven minus, minus twelve, is plus one. The leftover is positive. Step five. Read it off. The quotient is minus four. The remainder is one, not minus two. Now the congruence relation. This is a key idea for the whole chapter. Take two integers, a and b, and a positive integer m. We say a is congruent to b, modulo m, if m divides the difference, a minus b. We write it like this. a is congruent to b, mod m. The equals sign has three bars, not two. The number m is called the modulus. If they are not congruent, we put a slash through the three-bar sign.

This is a nice way to feel what congruence means. Two numbers are congruent mod m exactly when they leave the same remainder after dividing by m. So congruence sorts every integer into buckets, one bucket per remainder. The clock is the right picture. On a twelve hour clock, three o'clock and fifteen o'clock point at the same spot. The difference is twelve, and twelve is our modulus. So the two numbers are congruent mod twelve.

Let's test a congruence, step by step. Step one. Set it up. a is seventeen. b is five. The modulus m is six. Step two. The test asks whether m divides a minus b. So take the difference. Seventeen minus five is twelve. Step three. Does six divide twelve? Yes, because twelve is six times two. Step four. The test passes. So seventeen is congruent to five, mod six.

Now one that fails, so you can see the difference. Step one. Set it up. a is twenty four. b is fourteen. The modulus m is six. Step two. Take the difference. Twenty four minus fourteen is ten. Step three. Does six divide ten? No. Ten over six is not a whole number. Step four. The test fails. So twenty four is not congruent to fourteen, mod six.

Let's dig deeper into congruences. Theorem four. Let m be a positive integer. Then a and b are congruent, mod m, if and only if there is an integer k so that a equals b, plus k times m. In plain words, a and b are congruent exactly when they differ by a whole number of steps of size m. So you can hop from b to a by taking k steps, each of size m. The number k may be negative, which just means hopping backwards.

An if and only if has two directions, so we set it up as two little proofs. This is the first one. Line zero. Assume a is congruent to b, mod m. Step one. By the definition of congruence, m divides a minus b. Step two. If m divides a minus b, then a minus b equals k times m, for some whole number k. Step three. Move the b across to the other side. That gives a equals b, plus k times m. Direction one is done.

Now the second direction, and we run the same reasoning backwards. Line zero. Assume a equals b, plus k times m. Step one. Move the b across. So a minus b equals k times m. Step two. That says a minus b is exactly k copies of m. So m divides a minus b. Step three. And by the definition, that means a is congruent to b, mod m. Both directions hold, so the theorem is proved.

Careful. The word mod shows up in two ways, and they mean different things. In a is congruent to b, mod m, the word mod is part of a relation. It tells you whether two numbers are related, yes or no. But in a mod m equals b, the word mod is a function. It takes a number and gives back a remainder. Theorem three ties the two together. a is congruent to b, mod m, if and only if a mod m equals b mod m. So the relation holds exactly when the two functions give the same answer.

The proof is left as an exercise. This next result is very useful. Congruences behave well with addition and multiplication. Theorem five. Suppose a is congruent to b, mod m, and c is congruent to d, mod m. Then a plus c is congruent to b plus d, and a times c is congruent to b times d. The practical meaning is simple. Inside a congruence you may swap any number for another one congruent to it, and the congruence still holds.

Set it up first. By Theorem four, we can write b as a plus s times m, and d as c plus t times m, where s and t are whole numbers. Line zero. Look at the sum on the right hand side, b plus d. Step one. Put in what b and d are. Step two. Regroup. Collect a and c together, and pull the m out of the other two terms. We get a plus c, plus m times, s plus t. Step three. So b plus d and a plus c differ by a multiple of m. That is exactly what the congruence says.

Now the product, with the same set up. Line zero. Look at b times d. Step one. Put in what b and d are. So we have, in brackets a plus s m, times, in brackets c plus t m. Step two. Multiply it out. One term is a times c. Every other term has an m in it, so pull the m out. We get a c, plus m times, a t plus c s plus s t m. Step three. So b d and a c differ by a multiple of m. That means a c is congruent to b d, mod m.

Set it up. The modulus is five. Seven is congruent to two, mod five, because seven minus two is five. And eleven is congruent to one, mod five, because eleven minus one is ten. Now the sum. Line zero, seven plus eleven. Step one. Theorem five lets us swap each side for something congruent. So swap seven for two, and eleven for one. We get two plus one. Step two. That is three, mod five. Check it. Seven plus eleven is eighteen, and eighteen mod five is three. It agrees. Now the product.

Line zero, seven times eleven. Step three. Swap each side again. We get two times one. Step four. That is two, mod five. Check it. Seven times eleven is seventy seven, and seventy seven mod five is two. It agrees. When you have a true congruence, you may do two things safely. First, you may multiply both sides by any whole number, and it stays true. Second, you may add any whole number to both sides. That also keeps the congruence true. Both come straight from Theorem five, taking d equal to c. But dividing is a different story, and the next slide shows why.

Let's watch division break a congruence. Line zero. Fourteen is congruent to eight, mod six. This is true, because six divides fourteen minus eight, which is six. Step one. Divide the left side by two. Fourteen over two is seven. Step two. Divide the right side by two. Eight over two is four. Step three. Now test the new pair. Seven minus four is three. Step four. Does six divide three? No. So seven is not congruent to four, mod six. Dividing broke a true congruence.

In Section four point three we learn the exact condition that makes division safe. Sometimes the numbers are large. We do not want to add or multiply them in full. This corollary is a shortcut. To find the sum, mod m, first take each number mod m. Then add those small remainders. Then take mod m once more. The product works the same way. Take each number mod m, multiply the two small remainders, then take mod m again. The point is that every number stays small along the way.

Let's give this arithmetic a name. Z sub m is the set of whole numbers from zero up to m minus one. So for eleven, the set is zero through ten. Addition mod m: add the two numbers the normal way, then take mod m. Multiplication mod m: multiply the two numbers, then take mod m. Working this way is called arithmetic modulo m. The nice part is that every answer lands back inside the set.

Set it up. The modulus is eleven. So every answer must land in the set zero through ten. Line zero. We want seven plus, sub eleven, nine. Step one. By the definition, that is seven plus nine, then mod eleven. Step two. Seven plus nine is sixteen. Sixteen mod eleven is five, because sixteen is one eleven plus five. Now the product. Line zero, seven times, sub eleven, nine. Step three. By the definition, that is seven times nine, then mod eleven. Step four. Seven times nine is sixty three.

Sixty three mod eleven is eight, because sixty three is five elevens, fifty five, plus eight. Both answers sit inside the set, just as promised. This new arithmetic behaves a lot like normal arithmetic. First closure. If you add or multiply two members of the set, the answer is still in the set. It never escapes. Second associativity. The grouping does not matter. You may combine the first two first, or the last two first, and the result is the same. This holds for both plus and times.

Two more properties. Commutativity: the order does not matter. a plus b equals b plus a. And a times b equals b times a. Identity elements. Zero is the do nothing element for addition. Adding zero changes nothing. One is the do nothing element for multiplication. Multiplying by one changes nothing. Both live inside the set, so we never have to leave.

Two more. Additive inverse: every element has a partner that brings it back to zero. If a is not zero, its partner is m minus a. Add them and you get zero. And zero is its own partner. Distributivity: multiplication spreads over addition, just like normal. a times the sum of b and c equals a times b, plus a times c. The book leaves the proofs as exercises forty two through forty four.

Now a warning. Look back at that list of properties. One familiar thing is missing. We did not list multiplicative inverses, and they do not always exist. For example, take two, mod six. There is no number you can multiply by two, mod six, to land on one. Try them all. Two times zero is zero, two times one is two, two times two is four, two times three is zero, and so on. One never appears. So two has no multiplicative inverse mod six. In Section four point four we find the exact condition.

It depends on the greatest common divisor of a and m. Section four point two. Integer Representations and Algorithms. So far we studied divisibility and modular arithmetic. Now we ask a new question. How do we actually write down a number, and how does a computer store it? We will see base ten, base two, base eight, and base sixteen. We will also learn fast algorithms for adding, multiplying, and taking powers.

This is our plan for this section. First, integer representations. We will write numbers in base b, in binary, in octal, and in hexadecimal. Second, a base conversion algorithm, a step by step recipe to change a number from one base to another. Third, algorithms for integer operations, the methods a computer uses to add, multiply, and take powers.

We write numbers in base ten every day. Base ten means each place is a power of ten. Take nine hundred sixty five. The nine is in the hundreds place, so it means nine times ten squared. The six is in the tens place, so six times ten. The five is in the ones place, so five times one. Add them and you get nine hundred sixty five. But ten is not special. We can use any base b bigger than one. Three bases matter most for computers. Base two, called binary. Base eight, called octal.

And base sixteen, called hexadecimal. History is fun here. The ancient Maya used base twenty. The ancient Babylonians used base sixty. And that choice is still with us. Base sixty is the reason we count sixty minutes in an hour, and sixty seconds in a minute. A four thousand year old habit, still on your wrist. Why can we use any base b bigger than one? Because of Theorem one. Take any base b bigger than one. Then every positive integer n can be written as a sum. Each term is a digit times a power of b. The digits are whole numbers from zero up to b minus one. And the leading digit is not zero. The important word is uniquely. There is only one such expansion for each number. We prove this by induction in Section five point one.

Now the notation. The numbers a sub j are called the base b digits. The whole thing is called the base b expansion of n. We write it as the string of digits inside parentheses, with a small b below. So the little b at the bottom right tells you which base you are reading. For base ten we usually drop the little ten, because base ten is our default. Binary is base two. Most computers store integers this way. It uses only two digits, zero and one. Computers love binary, because a wire is either on or off. Nothing in between. Each place in a binary number is a power of two. Reading from the right, the places are one, two, four, eight, sixteen, and so on. Each step to the left doubles. To read a binary number, you add up the places that hold a one.

Set it up. Count the bits. There are nine of them. So the places run from two to the eighth, on the left, down to two to the zero, on the right. Line zero. This is the number we convert. Step one. Write the top four places. One times two to the eighth, plus zero times two to the seventh, plus one times two to the sixth, plus zero times two to the fifth. Step two. Now the low five places. All of them hold a one. Step three.

Throw away every place that holds a zero, and write the values of the rest. Two to the eighth is two hundred fifty six. Two to the sixth is sixty four. Then sixteen, eight, four, two, and one. Step four. Add them all up. The answer is three hundred fifty one. This one is shorter, so you can see the whole shape at once. Five bits, so the places run from two to the fourth down to two to the zero. Line zero. The number is one one zero one one, in binary. Step one. Write it place by place. One times sixteen, plus one times eight, plus zero times four, plus one times two, plus one times one. Step two. The four place holds a zero, so it drops out. That leaves sixteen plus eight plus two plus one. Step three. Add them. The answer is twenty seven.

Octal is base eight. It uses eight digits, zero through seven. There is no digit eight and no digit nine in octal. Each place is a power of eight. Reading from the right, the places are one, then eight, then sixty four, then five hundred twelve. Each step to the left multiplies by eight. Set it up. There are four digits, so the places are eight cubed, eight squared, eight, and one. Line zero. Our number is seven zero one six, in octal. Step one. Write it place by place. Seven times eight cubed, plus zero times eight squared, plus one times eight, plus six times one. Step two. Eight cubed is five hundred twelve. The second term is zero, so it drops out. Step three. Seven times five hundred twelve is three thousand five hundred eighty four. The small terms add to fourteen.

Step four. Add them. The answer is three thousand five hundred ninety eight. This one is small. One one one, in octal. Three digits, so the places are sixty four, eight, and one. Step one. Write it place by place. Step two. Work out the powers. Sixty four, plus eight, plus one. Step three. Add them. The answer is seventy three. Now notice something. The same three digits mean different things in different bases. One one one in octal is seventy three. But one one one in binary is only seven. The digits alone are not enough. You must know the base.

Hexadecimal is base sixteen. It needs sixteen different digits. But we only have ten number symbols, zero through nine. So we borrow six letters. Look at the table. A means ten. B means eleven. C means twelve. D means thirteen. E means fourteen. And F means fifteen. So the full digit set runs zero through nine, then A through F. Set it up. There are five digits, so the places run from sixteen to the fourth down to sixteen to the zero. And translate the letters first. A is ten. E is fourteen. B is eleven. Line zero. Our number is two, A, E, zero, B, in hexadecimal. Step one. Write the top three places. Two times sixteen to the fourth, plus ten times sixteen cubed, plus fourteen times sixteen squared. Step two. Then the low two places. Zero times sixteen, plus eleven times one. Step three. Work out each term.

One hundred thirty one thousand seventy two, plus forty thousand nine hundred sixty, plus three thousand five hundred eighty four, plus zero, plus eleven. Step four. Add them up. The answer is one hundred seventy five thousand six hundred twenty seven. One more short one. E five, in hexadecimal. Two digits, so the places are sixteen and one. Step one. E stands for fourteen. So we have fourteen times sixteen, plus five times one. Step two. Fourteen times sixteen is two hundred twenty four. Plus five. Step three. Add them. The answer is two hundred twenty nine.

Let's learn how to change a number into another base. Say we want the base b form of an integer n. The trick is simple. We divide again and again by b, and we keep the remainders. Step one. Divide n by b. We get a quotient q zero and a remainder a zero. That first remainder is the rightmost digit of the answer. Step two. Now take the quotient q zero and divide it by b. The new remainder a one is the next digit, the second one from the right. Step three. Keep going.

Each time, divide the newest quotient by b, and write down the remainder. Step four. Stop when the quotient reaches zero. Then read the remainders from the bottom back up to the top. This is the same method written as an algorithm. Let's read it slowly. We start by setting q to n, and k to zero. The variable q holds the current quotient. The variable k counts the digits. The while loop runs as long as q is not zero. Inside, we do two things. First, a sub k becomes q mod b. That is the remainder, our next digit. Second, q becomes q div b. That is the new quotient, the whole part of q divided by b. Then we add one to k. When q reaches zero, the loop stops.

We return the digits a sub k minus one down to a sub zero. Read together, they form the base b expansion of n. Set it up. Octal means base eight. We start with q equal to twelve thousand three hundred forty five, and we divide by eight each round. Round one. Twelve thousand three hundred forty five equals eight times one thousand five hundred forty three, plus one. The digit is one. Round two. Divide one thousand five hundred forty three by eight. It equals eight times one hundred ninety two, plus seven. The digit is seven. Round three. Divide one hundred ninety two by eight.

It equals eight times twenty four, plus zero. The digit is zero. Round four. Divide twenty four by eight. It equals eight times three, plus zero. The digit is zero. Round five. Divide three by eight. The quotient is zero, and the remainder is three. The quotient hit zero, so we stop. Now read off the answer. The remainders came out from right to left, so we read them from the bottom of the table back up to the top. That gives three, zero, zero, seven, one. So the octal form is three zero zero seven one. Always check your work. Three times eight to the fourth is twelve thousand two hundred eighty eight. Seven times eight is fifty six. Plus one. Add them and you get twelve thousand three hundred forty five, which is the number we started with. It checks out.

This table lines up four bases side by side, for the numbers zero through seven. The top row is decimal, our everyday base ten. Then hexadecimal, base sixteen. For zero through seven it looks exactly the same. Then octal, base eight. Again the same digits. And last, binary, base two. Binary runs out of digits after one. It writes two as one zero, and four as one zero zero. So for the first eight numbers, decimal, hexadecimal and octal all agree. Nothing has run out yet.

They only differ once a base runs out of digits. Now the second half, eight through fifteen. Here the bases part company. The top row is decimal again, eight through fifteen. Look at hexadecimal. After nine it needs new symbols, so it uses letters. A stands for ten, B for eleven, C for twelve, D for thirteen, E for fourteen, and F for fifteen. Look at octal. It ran out at seven, so eight is written one zero. Then it runs on up to one seven for fifteen. Look at binary. Every number from eight to fifteen takes exactly four bits, from one zero zero zero up to one one one one.

This is the key point behind that table. Eight is two cubed. So each octal digit is worth exactly three binary digits. Sixteen is two to the fourth. So each hexadecimal digit is worth exactly four binary digits. Because of this neat fit, changing between binary, octal, and hexadecimal needs no arithmetic at all. You only regroup the bits. Group them in threes for octal, or in fours for hexadecimal.

Let's find the octal form of a long binary number. Step one. Set it up. Octal needs blocks of three bits, and we always count from the right. Our number has fourteen bits, which is not a multiple of three. The next multiple up is fifteen. Step two. So we pad one zero on the front, then cut the bits into blocks of three. We get zero one one, one one one, zero one zero, one one one, one zero zero. Step three. Read each block as a single octal digit. Zero one one is three. One one one is seven.

Zero one zero is two. One one one is seven again. One zero zero is four. Step four. Write them in order. The octal answer is three seven two seven four. Same bits, different grouping. Now we want the hexadecimal form. Step one. Set it up. Hexadecimal needs blocks of four bits, again counted from the right. We have fourteen bits, so we round up to sixteen. Step two. Pad two zeros on the front, then cut into fours. We get zero zero one one, one one one zero, one zero one one, one one zero zero. Step three. Read each block as one hex digit. Zero zero one one is three. One one one zero is fourteen, which is E. One zero one one is eleven, which is B.

One one zero zero is twelve, which is C. Step four. Write them in order. The hexadecimal answer is three, E, B, C. Notice how the very same bits gave two different answers, just by grouping them in threes or in fours. Computer chips work in binary, so we need algorithms that add binary numbers bit by bit. Each binary digit is called a bit. This is the addition algorithm. We add two numbers a and b, given by their bits. We keep a carry, called c, and set it to zero at the start. The for loop walks through the bit positions, from j equals zero up to n minus one, that is from right to left. At each position we add three things: bit a sub j, bit b sub j, and the carry c. From that sum we get two pieces.

The new carry d is the floor of the sum divided by two. Floor means round down. The sum bit s sub j is the sum minus two times d. Then the carry c becomes d, ready for the next position. After the loop, the final carry becomes the top bit, s sub n. Then we return the bits the way we write numbers, top bit first. Now, how much work is that? Adding two n bit integers takes big O of n bit additions. Why? At each bit position we do the same small job. Add three bits, work out one carry, write one sum bit. Nothing in that job gets bigger as the numbers get longer. And there are n positions. So adding twice as many bits takes about twice as long.

Now the multiplication algorithm. We multiply two numbers a and b, again given by their bits, using shifted partial products. The first loop builds the partial products. For each bit position j of b, we look at bit b sub j. If that bit is one, the partial product c sub j is a shifted left by j places. Shifting left by j places is the same as multiplying a by two to the j. If the bit is zero, the partial product c sub j is just zero.

So we get n partial products, c sub zero up to c sub n minus one. The second loop adds them all together. We start p at zero, then add each partial product in turn. At the end, p holds the product a times b. And what does multiplication cost? Multiplying two n bit integers takes big O of n squared bit additions. Count it in two parts. There are n partial products, one for each bit of b. And adding each partial product costs about n bit operations, as we just saw. Multiply the two counts together. n times n is n squared. So multiplication is much more expensive than addition.

In cryptography we often need b to the power n, mod m, where b, n, and m are all huge. We need a fast way to do this. The trick uses the binary form of the exponent n. Look at the boxed identity. Because the exponent n is a sum of powers of two, b to the n splits into a product. Each factor is b raised to one of those powers of two. And a factor only appears when the matching bit is a one. So the power splits into one factor per bit that is one.

So to build b to the n, we only need a short list of values. Start with b. Square it to get b squared. Square that to get b to the fourth. Square again for b to the eighth, and keep going. Each one is the square of the one before, so each costs a single multiplication. Then we multiply together just the terms where the matching bit is one, and skip the rest. That is why this is fast. Instead of n multiplications, we need about log n of them.

Set it up. Eleven in binary is one zero one one. The bits that are one sit at positions zero, one, and three. Position three means three to the eighth. Position one means three squared. Position zero means three to the first. Line zero. We want three to the eleventh. Step one. So it equals three to the eighth, times three squared, times three. Check the exponents: eight plus two plus one is eleven. Step two. We build three to the eighth by squaring three times. Three squared, squared, squared.

Step three. Three squared is nine. Nine squared is eighty one. Step four. Eighty one squared is six thousand five hundred sixty one, which is three to the eighth. Step five. Multiply the three pieces. The answer is one hundred seventy seven thousand one hundred forty seven. And notice the cost. We used three squarings and two more multiplications. That is five in all, instead of the ten we would need by multiplying by three over and over.

This algorithm puts the idea to work, and it keeps every number small by taking mod m at each step. We keep two variables. The answer x starts at one. The variable power starts at b mod m. The for loop runs over the bits of n, from bit zero up to bit k minus one. At each step we check the current bit a sub i. If that bit is one, we fold power into the answer. x becomes x times power, mod m. Then, whether the bit was one or zero, we square power.

So power moves from b, to b squared, to b to the fourth, and so on, each time reduced mod m. When the loop ends, x holds b to the n, mod m. How fast is this? The cost is order, the square of log m, times log n, bit operations. First, every number stays below m, because we reduce mod m after each multiplication. So the numbers never grow, however large the exponent is. Second, the loop runs once per bit of the exponent n, which is about log n times, not n times. That is the difference between a few thousand steps and a hopeless number of steps. This is what makes public key cryptography practical.

Section four point three. Primes and Greatest Common Divisors. Now we turn to the building blocks of the integers. A prime number cannot be split into a product of smaller numbers. We will study primes, some famous open questions about them, and the greatest common divisor of two numbers. This is our plan for this section. First we meet prime numbers and their main properties. Then we look at some famous conjectures and open problems about primes, questions that are still not answered. Next we study the greatest common divisor and the least common multiple of two numbers. After that comes the Euclidean algorithm, a fast way to find the greatest common divisor. Finally, we see how to write a greatest common divisor as a linear combination of the two numbers.

Let's define a prime number. A prime is a positive integer p, bigger than one, whose only positive factors are one and p itself. In other words, nothing else divides it evenly. A positive integer bigger than one that is not prime is called composite. Composite means it can be broken into smaller factors. And notice that one is neither prime nor composite. It is left out on purpose, and we will see why shortly.

Let's test two numbers, step by step. Step one. Set it up. To decide, we hunt for a factor strictly between one and the number itself. If we find one, the number is composite. Step two. Try seven. Test two, three, four, five, and six. None of them divides seven evenly. Step three. So only one and seven are left as factors. Seven is prime. Step four. Now try nine. Two does not divide nine. But three does. Nine equals three times three. Step five. We found a factor other than one and nine.

So nine is composite. This is one of the biggest ideas in number theory. Take any whole number bigger than one. You can break it into prime pieces. And there is only one way to do it, once you list the pieces from small to large. We call this the unique prime factorization. Think of primes as atoms. Every number is built from these atoms, and the recipe is unique.

Let's see the theorem at work, one number at a time. One. One hundred equals two times two times five times five. We write that as two squared times five squared. Two. Six hundred forty one is a prime, so it stands alone. To be sure, we try the primes up to twenty five: two, three, five seven eleven thirteen, seventeen, nineteen, and twenty three. None of them divides it. And twenty six squared is already bigger than six hundred forty one, so there is nothing left to try. Three.

Nine hundred ninety nine equals three times three times three times thirty seven. That is three cubed times thirty seven. Four. One thousand twenty four is two multiplied by itself ten times, which we write as two to the tenth. Same number, one factorization each. No other recipe exists. This is a simple, old method to find primes, named after Eratosthenes, a Greek scholar. The word sieve means a filter, like a kitchen strainer. It keeps the good bits and lets the rest fall through. Write the numbers from two to one hundred. We leave one off the list, because one is neither prime nor composite. Step a. Keep two, but cross out every other multiple of two: four, six, eight, and so on. Step b. Keep three, but cross out every other multiple of three. Step c.

Keep five, and cross out its other multiples. Step d. Keep seven, and cross out its other multiples. Step e. Now stop. Every number still standing is prime. In a moment we will see why four rounds were enough. Eratosthenes lived in the third century before the common era. He ran the great library at Alexandria, and he also measured the size of the Earth. His sieve still works today. And this is what it gives us. The primes up to one hundred are two, three, five, seven, eleven, thirteen, seventeen, nineteen, twenty three, twenty nine, thirty one, thirty seven, forty one, forty three, forty seven, fifty three, fifty nine, sixty one, sixty seven, seventy one, seventy three, seventy nine,

eighty three, eighty nine, and ninety seven. Twenty five primes in the first hundred numbers. Look at the grid. It shows one to one hundred, in ten rows of ten. The green cells are the primes. The grey cells are composite numbers that the sieve crossed out. The cell for one is pale, because one is neither prime nor composite. Notice how the green cells thin out as you go down the grid. The primes get rarer, but they never stop.

Now, why did we only sieve with two, three, five, and seven? This is the key fact. If a number n is composite, then it must have a prime divisor no bigger than the square root of n. The reason is simple. Suppose n equals a times b. The two factors cannot both be larger than the square root of n, because their product would then be larger than n. So at least one of them is small. Apply that here. The square root of one hundred is ten.

So every composite number up to one hundred has a prime factor below ten. The primes below ten are two, three, five, and seven. Four rounds, and we are done. That same fact gives us a test, called trial division. Take a number n bigger than one. Try every integer i from two up to the square root of n, and check whether it divides n. We start at two, because one divides everything and would tell us nothing. If none of them divides n, then n is prime. Trial division is correct, and it is easy to write. But it is very slow when n is large. For a number with two hundred digits, the square root still has one hundred digits, and no computer can count that far. That is exactly why cryptography needs the cleverer tests we meet later.

Can we ever run out of primes? No. Euclid showed there are infinitely many, and his proof is famous. Let's set it up carefully. We argue by contradiction. That means we assume the opposite of what we want, and then show that the assumption breaks. So suppose, for a moment, that there were only finitely many primes. Then we could list every one of them: p one, p two, and so on up to p n. Remember that list. On the next slide we build a number that ruins it.

Now the working, one step at a time. Step one. Build a new number q. Multiply every prime on the list together, then add one. Step two. Think about q. By the Fundamental Theorem of Arithmetic, q is either prime, or it breaks into primes. Step three. Now the trick. None of our listed primes can divide q. If some prime p j divided q, it would also have to divide q minus the product of all the primes. But that difference is exactly one. And no prime divides one. Step four.

So there must be a prime that is not on our list. Either q itself is that prime, or one of its prime factors is. Step five. That breaks our assumption that the list held every prime. The assumption was wrong. So there are infinitely many primes. This proof is more than two thousand years old. It comes from Euclid's Elements. Mathematicians still admire it, because it is short, complete, and surprising. Paul Erdos, a Hungarian mathematician of the twentieth century, liked to speak of The Book. In his joke, God keeps a book that holds the most perfect proof of every theorem. A mathematician's highest praise was to say that a proof was from The Book. Euclid's proof is the first entry.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent