COMPSCI 120:
Mathematics for Computer Science

Numbers

Exercise 1.1.Link copied Take an 8×88 \times 8 chessboard. Suppose you have a bunch of 2×12 \times 1 dominoes lying around. Can you completely cover your chessboard with dominoes, so that the dominoes don’t overlap or hang off of the board?

Now, suppose you have a younger sibling who has eaten the top-left square of your chessboard. Can you completely cover without overlap this chessboard with 2×12 \times 1 dominoes?

What if they also ate the bottom-right square? Does this change your answer?

Exercise 1.2.Link copied A mistake people often make when adding fractions is the following:
1x+1y=?1x+y\dfrac{1}{x} + \dfrac{1}{y} \stackrel{?}{=} \dfrac{1}{x+y}

Now, we know that this isn’t right, and that 1x+1y\dfrac{1}{x} + \dfrac{1}{y} is actually yxy+xxy=x+yxy\dfrac{y}{xy} + \dfrac{x}{xy} = \dfrac{x+y}{xy} . Sometimes, however, even a formula that is wrong in general might be right in a specific situation! Think of the old adage “a stopped clock is right twice a day”.

Does this ever happen with this mistake? In other words: are there any values x,yx,y such that 1x+1y=1x+y\dfrac{1}{x} + \dfrac{1}{y} = \dfrac{1}{x+y} ? Or is this false for literally every value of xx and yy ?

Integers

We start our coursebook by studying the integers! In case you have not seen the word “integer” before, we define it here:

In this course and in mathematics in general, we will define lots of useful concepts. When we do so, we’ll label these things by writing ”Definition” in bold, and then give you a carefully-written and precise definition of that word.

When studying for this class, it is a good idea to make sure that you know all of the definitions in this coursebook, as well as some examples and nonexamples for each definition where appropriate.

Definition 1.1.Link copied The integers are the collection of all whole numbers: that is, they consist of the whole positive numbers 1,2,3,4,-1,-2,-3,-4,\ldots , together with the whole negative numbers 1,2,3,4,...-1,-2,-3,-4,... , and the number 00 . We denote this set by writing the symbol Z\mathbb{Z} .

The symbol Z\mathbb{Z} comes from the German word “Zahl”, which means “number”, in case you were curious.

Example 1.1. The numbers 1,7,10000,78,45,0,3456781, -7, 10000, 78, 45, 0, -345678 are integers, but things like 2,2.787878787,18\sqrt{2}, -2.787878787, \frac{1}{8} and π\pi are not.

In some form or another, integers have been used by humans for almost as long as humans have existed theirselves. The Lebombo bone, one of the oldest human artifacts, is a device on which people used tally marks to count the number of days in the lunar cycle. The base-10 system and idea of numerals took a bit longer for us to discover, but we can trace these concepts back to at least the Egyptians and Sumerians around 4,000-3,000 BC. Finally, negative numbers and zero have been with us for at least two millenia: historical documents tracking back to at least 1000 BC describe how Chinese mathematicians used negative numbers solve concrete problems on bounding areas of fields, exchanging commodities, and debt.

In short, integers are quite handy! To this day, we use them to model all sorts of problems. To give a pair of examples, let’s solve the first two exercises from this chapter:

Answer to Exercise 1.1. By just trying things out by hand, it’s relatively straightforward to find a tiling of a 8×88 \times 8 board with no squares missing; one solution is drawn below.

When you remove a square, however, things get a bit more interesting! Try it for a while; no matter how you do this, you’ll always have at least one square left over uncovered.

However, saying “I tried it a lot and it didn’t work” is not a very persuasive argument. Suppose that you had a boss that said that Chad from Marketing said this was totally possible, and to have a working version on their desk by 5pm or you’ll be fired. What do you do?

Well, maybe you start revising your resumé and looking for other work. Before that, though, you may want to try to make a logical argument to your boss that shows that it’s impossible to come up with such a tiling - a set of reasons so airtight that no matter what they think or would respond with, they’d have to agree that you’re right. In mathematics and computer science, we call such logical arguments proofs!

In this class, we’re going to write these kinds of arguments for almost all of the claims that we make. We’re going to leave the rules for what constitutes a “proof” a little vague at first; in our first few chapters, we’re going to just try to write a solid argument that tells us why something is true, and anything that does that we’ll count as a proof. (Later in this course, we’ll approach proofs a bit more formally: feel free to read ahead if you can’t wait!)

For this problem, let’s prove that an 8×88 \times 8 chessboard without a single square cannot be covered with 2×12 \times 1 dominoes as follows:

Proof.

First, notice that each time we place a 2×12 \times 1 domino on a board, it covers two squares worth of area. Because dominoes cannot overlap, this means that if we have placed kk dominoes on our board, there are 2k2 k squares of our board covered by dominoes.

If we have an 8×88 \times 8 chessboard that’s missing a square, that board has 881=638 \cdot 8 - 1 = 63 squares on it in total. Therefore, if we’ve completely covered our chessboard with dominoes, the number kk of dominoes we used must satisfy the equation 2k=632k=63 .

In other words, we have k=31.5k=31.5 ; i.e. we’ve used half of a domino! This means that the other half of that domino is sticking off of our board or overlaps another domino, which means we’ve broken the rules of our tiling problem. So this is impossible! \Box

To denote the end of our arguments, we draw a square box symbol, like \Box . This means that we’ve reached the end of our argument. In physics or other fields, people often use QED in place of this. Feel free to use either in your own working!

Notice how in the argument above, the only things we used were factual observations (i.e. the number of squares in our 8×88 \times 8 grid minus a square, the amount of area taken up by each 2×12 \times 1 domino) and logical combinations of those observations. This is how you write a solid argument!

To get a bit more practice with this sort of thing, let’s try the last part of our exercise. In this problem, we had a chessboard with its top-left and bottom-right squares removed, and wanted to try to cover this with 2×12 \times 1 dominoes.

On one hand, the argument above no longer tells us that this is impossible. A 8×88 \times 8 board minus two squares contains 62 squares in total; therefore, it might be possible to do this with 31 dominoes! However, if you try this for a while, you’ll find yourself quite stuck again. So: why are we stuck? What is a compelling argument we could write that would persuade someone that this is truly impossible, and not just that we’re bad at tiling?

After some thought and clever observations, you might come up with the following:

Proof.

  1. Notice that each time we place a 2×12 \times 1 domino on our grid, it’s not just true that it covers two units of area: it also covers exactly one unit of white area and one unit of black area! This is because our chessboard has alternating white and black squares.
  2. Because dominoes cannot overlap, this means that if we have placed kk dominoes on our board, there are kk white squares and kk black squares covered by our dominoes. In particular, this means that we always have as much black area covered as white area!

  3. However, if we count the black and white area in our 8×88 \times 8 board with the top-left and bottom-right squares removed, we can see that there are 32 black squares and 30 white squares. These numbers are different! Therefore, it is impossible to cover this board with dominoes as well.

Notice that when we wrote this argument, we used observations from our earlier work, and modified them to suit this new problem! This is a good problem-solving technique in general: always see if you can modify a pre-existing solution before trying to come up with something entirely new.

In our working above, the fact that we could not write 63 as an integer multiple of 2 was a very useful observation! We can generalize this into the concepts of even and odd integers:

Even and Odd Integers

Definition 1.2.Link copied We say that an integer is even if we can write it as 2 times another integer; in other words, we say that an integer nn is even if we can find an integer kk such that n=2kn = 2k .Similarly, we say that an integer is odd if we can write it as one plus an even number; in other words; we say that an integer nn is odd if we can find an integer kk such that n=2k+1n = 2k + 1 .

Example 1.2. 2 is even, because we can write 2=212 = 2 \cdot 1 , and 11 is an integer. Similarly, 44 is an even number, because we can write 4=224 = 2 \cdot 2 and 22 is an integer.

As well, 2,4-2, -4 and 6-6 are all even, because we can write 2=2(1)-2 = 2 \cdot (-1) , 4=2(2)-4 = 2\cdot (-2) and 6=2(3)-6 = 2 \cdot (-3) , where 1,2,3-1,-2,-3 are all integers.

3 is odd, because we can write 3=2+13 = 2+1 and 2 is even (as shown above.) Similarly, because 5=4+15 = 4+1 and 7=6+17=6+1 , we have that 5 and 7 are also odd. As well, 1,3,5-1, -3, -5 are all odd, as 1=2+1,3=4+1,-1 = -2 +1, -3 = -4+1, and 5=6+1-5 =-6+1 .

Exercise 1.3.Link copied Using the definition above, is 0 even or odd?

To get some practice writing logical arguments, let’s look at a few properties of even and odd numbers that you already know:

Claim 1.1. The sum of any two odd numbers is even.

Before getting into a “good” solution for this claim, let’s first study an argument that doesn’t work.

“Bad” solution: Well, 1+1=21+1 = 2 is even, 3+7=103+7 = 10 is even, 13+5=8-13 + 5 = -8 is even, and 1001+2077=30781001 + 2077 = 3078 is even. Certainly seems to be true! \Box

A defense of the “bad” solution: This might seem like a silly argument, but suppose we’d listed a thousand examples, or a billion examples, or set a computer program to work overnight and had it check all of the pairs of numbers below 101210^{12} . In many other fields of study, that would be enough to “show” a claim is true! (Think about science labs: there, we prove claims via experimentation, and any theory you could test a billion times and get the same result would certainly seem very true!)

Why this argument is not acceptable in mathematics and computer science: When we make a claim about “any” number, or say that something is true for “all” values, we want to really mean it. If we have not literally shown that the claim holds for every possible case, we don’t believe that this is sufficient!

This is not just because computer scientists are fussy. In the world of numbers, there are tons of “eventual” counterexamples out there:

Skip ahead a bit to Definition 1.4. if you haven’t encountered prime numbers before!

If you were to just go through and check things by hand, you’d probably be persuaded by the first few entries: 12=34,121=1111,1211=1737,12111=36711,12111=431281,12 = 3\cdot 4, 121 = 11 \cdot 11, 1211 = 173 \cdot 7, 12111 = 367 \cdot 11, 12111 = 431 \cdot 281, \ldots

However, when you get to 121111136 1s12\overbrace{111\ldots1}^{136~1's} , that one’s prime! This is well beyond the range of any reasonable human’s ability to calculate things, and yet something that could come up in the context of computer programming and information security (where we make heavy use of 500+ digit primes all the time.)

Here’s a fun exercise one of the writers of this coursebook saw in the wild on Facebook, shared by one of our troll-ier friends:

Exercise 1.4.Link copied (++) Can you find positive integer values for 🐐,🦜🐐, 🦜 and 🐍🐍 so that the equation 🐍🐐+🦜+🐐🐍+🦜+🦜🐍+🐐=4\frac{🐍}{🐐+🦜} + \frac{🐐}{🐍+🦜} + \frac{🦜}{🐍+🐐} = 4 holds?

On its surface, this looks simple, right? Just solve it.

And yet: suppose you set a computer program to work at this problem, by just bashing out all possible triples of numbers. To be fair, let’s give you access to the world’s fastest supercomputer. By the end of a week, you wouldn’t have found an answer. By the end of a year, you wouldn’t have found an answer! Indeed: by the time we reached the heat death of the universe, you’d still have found nothing. You’d be tempted to say that no answer exists, right?

Roughly 1010610^{106} years, or 3101133 \cdot 10^{113} seconds, or about 6101306 \cdot 10^{130} floating-point operations on the world’s fastest supercomputer as of early 2019.

And yet, an answer exists! The math required to find this goes way beyond the scope of this course, but if you’re curious: the smallest known answer is to do the following:

🐍=154476802108746166441951315019919837485664325669565431700026634898253202035277999,🐍 =154476802108746166441951315019919837485664325669565431700026634898253202035277999, 🐐=36875131794129999827197811565225474825492979968971970996283137471637224634055579,🐐= 36875131794129999827197811565225474825492979968971970996283137471637224634055579, 🦜=4373612677928697257861252602371390152816537558161613618621437993378423467772036.🦜= 4373612677928697257861252602371390152816537558161613618621437993378423467772036.

In general, mathematics and computer science is full of things like this! Huge numbers that are prime or that satisfy equations similar to the ones above can be incredibly useful in information security (as you’ll see in later papers in Compsci/Maths!)

Examples alone, then, are not enough for a good argument. What would a good solution look like here? Here is one of many possible answers:

Claim 1.1.Link copied The sum of any two odd numbers is even.

Proof.

Take any two odd numbers. Let’s give them names, for ease of reference: let’s call them MM and NN . By definition, because MM and NN are odd, we can write M=2k+1M = 2k+1 and N=2l+1N = 2l+1 , for two integers k,lk, l .

Therefore, M+N=(2k+1)+(2l+1)=2k+2l+2=2(k+l+1)M+N = (2k+1) + (2l+1) = 2k+2l + 2 = 2(k+l+1) . In particular, this means that M+NM+N is an even number, as we’ve written it as a multiple of 2!

The argument above might look a little awkwardly-written to you at a first glance! This is because mathematics is something of a language in its own right: there are certain phrases and constructions with very specific meanings in mathematics, that we use when making arguments to ensure that they’re completely rigorous. Let’s talk about some of those phrases and concepts that came up in the argument above:

Note also that we defined these variables only after saying what kind of object they were! This is like when you’re writing code: you typically have to declare the type of variable you’re working with, i.e. you’d write something like int numA instead of just saying numA.

To get a bit more practice with this, let’s try out a few more claims:

Claim 1.2.Link copied The product of any two odd numbers is odd.

Proof.

As before, let’s start by taking any two odd numbers. Let’s give them names, and call them MM and NN respectively.

Also as before, let’s consult our definitions! By definition, because M,NM, N are both odd, we can again write M=2k+1M = 2k+1 and N=2l+1N = 2l+1 , for two integers k,lk, l .

Therefore, MN=(2k+1)(2l+1)M \cdot N = (2k+1) \cdot (2l+1) . Expanding this product gives you 4kl+2k+2l+14kl + 2k + 2l + 1 , which you can regroup as (4kl+2k+2l)+1(4kl+ 2k + 2l) + 1 . Factoring a 2 out of the left-hand part gives you 2(2kl+k+l)+12(2kl + k + l) + 1 .

Because k,lk, l are integers, the expression 2kl+k+l2kl + k + l inside the parentheses is an integer as well, as any product or sum of integers is still an integer. Therefore, we’ve written MNM \cdot N in the form 2(an integer)+12 \cdot (\text{an integer}) + 1 ; i.e. we’ve shown that MNM \cdot N satisfies the definition of being an odd integer! \Box

Claim 1.3.Link copied No integer is both even and odd at the same time.

Proof.

As before, let’s start by working in general. Take any integer NN .

Phrases like “Take any integer nn ” or “take any number xx ” are useful; they start by both saying that we’re going to work in general with any number, and also assign a variable to that number so that we can refer to it.

As a thought experiment, let’s think about what it would mean for NN to be both odd and even at the same time.

If NN was even, then by definition we could write N=2kN = 2k for some integer kk ; as well, if NN is also odd, then by definition we should be able to write N=2l+1N = 2l + 1 for some integer ll .

Note that we had to pick different letters k,lk, l when we applied the even and odd definitions! If we had used the same letter kk for both, that would imply that the same integer is being used in both definitions, and this might not be true.

As a result, we have N=2kN = 2k and N=2l+1N = 2l+1 . Combining gives us 2k=2l+12k=2l+1 , which we can rearrange into 2(kl)=12(k-l) = 1 ; i.e. kl=12k-l = \frac12 .

But this is clearly not possible! kk and ll are both integers, and so their difference must an integer as well; it cannot be 12\frac12 !

As a result, we’ve shown that it is impossible for an integer NN to be equal to 2k2k and 2l+12l+1 at the same time if k,lk, l are both integers; that is, it is impossible for NN to be both odd and even! \Box

By definition, you can think of our even/odd split above as classifying every number as either “a multiple by 2” or “not a multiple of 2.” We expand on this idea in the following section, where we study divisibility and prime numbers.

Divisibility and Primes

Definition 1.3.Link copied Given two integers a,ba,b , we say that aa divides bb if there is some integer kk such that ak=bak = b .

There are many synonyms for “divides”: each of the phrases

all mean the same thing as ”aa divides bb .”

Here’s a string of examples, to make this clearer:

Example 1.3.

Example 1.4. Note that the phrases “divides” and “can be divided by,” while quite similar-sounding in English, have almost the opposite meaning in mathematics! For instance, 3 divides 9 and 9 can be divided by 3 are the same claims.

To get some practice with making abstract arguments about divisibility, let’s study a simple claim about factors and divisibility:

Claim 1.4.Link copied Let a,b,ca,b,c be three integers. If aa divides bb and bb divides cc , then aa divides cc .

Again, we need to work in abstract! That is: we cannot just check this for a few values and say that “well, when a=4,b=12,c=36a=4, b=12, c=36 this all works out.” Instead, we must consider any three integers a,b,ca,b,c , and work in general without knowing what a,b,ca,b,c are.

In theoretical computer science / mathematics, the words “any” and “every” are often used interchangeably. That is, if someone says to you “For any integer nn , n20n^2 \geq 0 ,” this means the same thing as “For every integer nn , n20n^2 \geq 0 .”

By definition, if aa divides bb , we can write ak=bak = b for some integer kk . Similarly, if bb divides cc , then we can write bl=cbl = c for some integer ll .

Now, take the equation ak=bak=b , and use this to substitute in akak for bb in our second equation bl=cbl=c . This gives us akl=cakl = c , i.e. a(kl)=ca(kl) = c . Because k,lk, l are both integers, their product is an integer; as a result, we’ve written a(an integer)=ca \cdot (\textit{an integer}) = c . In other words, by definition we have shown that aa is a factor of cc , as desired.

A particularly useful concept related to divisibility is the concept of a prime number:

Definition 1.4.Link copied A prime number is any positive integer with only two distinct positive factors; namely, 1 and itself.

Example 1.5. The first few prime numbers are 2,3,5,7,11,13,17,19,23,29,2,3,5,7,11,13,17,19,23,29, \ldots

Observation 1.1.Link copied 1 is not a prime number.

Proof.

This is because 1’s only factor is 1, and so 1 does not have two distinct positive integer factors.

Observation 1.2.Link copied 2 is the only even prime number.

Proof.

This is because every other positive even number by definition has the form 2k2k , and so has at least 1,2,k,2k1,2,k, 2k as its set of factors (and thus has more than 2 distinct positive factors.)

Definition 1.5.Link copied A composite number is any positive integer nn that can be written as the product of two integers a,ba,b , both of which are at least 2 (and thus both of which are strictly smaller than nn .)

Example 1.6. 6=236 = 2 \cdot 3 , 9=339 = 3 \cdot 3 , and 24=21224 = 2 \cdot 12 are all composite.

Observation 1.3.Link copied By definition, any positive integer is either a prime number, a composite number, or 1.
Definition 1.6.Link copied Given a positive integer nn , a prime factorization of nn is any way to write nn as a product of prime numbers.

Example 1.7. Here are a few prime factorizations:

Here are a pair of useful observations about prime numbers:

Theorem 1.1. Every positive integer can be factorized into a product of prime numbers in exactly one way, up to the ordering of those prime factors.

To see a proof of Theorem 1.1., take Compsci 225!

You might object to Theorem 1.1. by saying that 1 cannot be factored into a product of prime numbers. If so, good thinking! For now, regard 1 as a special case.

Later on, though, we’ll consider the idea of an “empty product” when we get to the factorial and exponential functions. At that time, we’ll argue that the “empty product” or “product of no numbers” should be considered to be 1, because 1 is the multiplicative identity. If you believe this, then 1 can indeed be written as the product of prime numbers; it specifically can be written as the product of no prime numbers!

In other words, this theorem is saying the following:

Proving this theorem is a bit beyond our skill set at the moment. Instead, let’s mention a second useful fact about primes:

Theorem 1.2. There are infinitely many primes.

This proof is also a bit beyond us for now. However, if you skip ahead to the proof by contradiction section of our notes / wait a few weeks, you’ll see a proof of this in our course!

Prime numbers (as you’ll see in Compsci 110) are incredibly useful for communicating securely. Using processes like the RSA cryptosystem, one of the first public-key cryptosystems to be developed, you can use prime numbers to communicate secretly over public channels.

Prime numbers are also quite baffling objects, in that despite having studied them since at least 300 BC there are still so many things we do not know about them! Here are a few particularly outstanding problems, the solutions for which would earn you an instant Ph.D/professorship basically anywhere you like in the world:

Exercise 1.5.Link copied (++) Goldbach conjecture. Show that every even integer greater than 2 can be written as the sum of two prime numbers. For example, we can write 8=3+5,14=11+3,24=17+7,6=3+3,8=3+5, 14 = 11+3, 24 = 17+7, 6=3+3, \ldots
Exercise 1.6.Link copied (++) Twin prime conjecture. A pair of prime numbers are called twin primes if one is exactly two larger than the other. For example, (5,7)(5,7) , (11,13)(11,13) and (41,43)(41,43) are twin primes. Show that there are infinitely many twin primes.

In general, working with prime numbers can get tricky very fast; even simple problems can get out of hand! With that said, there are some claims that are approachable. We study two such statements here:

Claim 1.5.Link copied Let abab be a two-digit positive integer (where bb is that number’s ones’ digit and aa is its tens’ digit.) Show that the number abababab is not prime.

Proof.

As noted before, examples alone aren’t enough for a solution. However, if you’re stuck (as many of us would be on first seeing a problem like this) they can be useful for helping us find a place to start!

abab abababab prime factorization of abababab
1010 10101010 251012 \cdot 5 \cdot 101
9898 98989898 2721012 \cdot 7^2 \cdot 101
2121 21212121 371013 \cdot 7 \cdot 101
8888 88888888 23111012^3 \cdot 11 \cdot 101
9292 92929292 22231012^2 \cdot 23 \cdot 101
4343 43434343 4310143 \cdot 101

So: let’s create a bunch of two-digit numbers and factor them (say, via WolframAlpha.) If our claim is wrong, then maybe we’ll stumble across a number whose only factors are 1 and itself. Conversely, if our claim is right, maybe we’ll see a pattern we can generalize! We do this at right.

As we do this, a pattern quickly emerges: it looks like all of these numbers are multiples of 101! This isn’t a solution yet: we just checked six numbers out of quite a few possibilities. It does, however, tell us how to write a proper argument here:

Notice that for any two-digit number abab , ab101=ab100+ab=ab00+ab=ababab \cdot101 = ab \cdot 100 + ab = ab00 + ab = abab . Therefore, any number of the form abababab is a multiple of 101 and also a multiple of abab , by definition. Because abab is a two-digit number by definition, this means that abababab has at least two factors other than 1. This means that abababab cannot be a prime number, as claimed! \Box

Claim 1.6.Link copied Let nn be any positive integer greater than 1. Let k=nk = \lfloor \sqrt{n} \rfloor denote the number given by rounding down n\sqrt{n} . If nn does not have any of the numbers 2,3,k2,3,\ldots k as factors, then nn is prime.

Proof.

We use the “suppose we’re wrong” technique from Claim 1.3. That is: take any number nn with the “no factors in the set 2,3,k2,3,\ldots k ” property listed above. There are two possibilities:

Therefore the only possibility left is that nn is prime, as desired. \Box

This last claim is particularly useful! We know that by definition, a positive integer nn is prime if it has exactly two positive integer factors: 1 and itself. Naively, then, to check if a number nn is a prime, you might think that you would have to check all of the numbers 2,3,4,n12,3,4,\ldots n-1 each individually, and see if any of them divide nn .

However, Claim 1.6. tells us that we don’t actually have to check all the numbers up to n1n-1 ; we only have to go up to n\sqrt{n} . This can save us a lot of time: as you’ll see later in this coursebook in our runtime-analysis section / in papers like Compsci 130 and 220, a “runtime” of checking n\sqrt{n} cases is considerably better than a “runtime” of checking nn cases!

For instance, to see if n=101n=101 is prime, Claim 1.6. tells us that because 101 is not a multiple of 2,3,4,5,6,7,8,9 or 10, then 101 is itself a prime. (Much faster than checking all of the numbers up to 100.)

We leave a few more exercises along these lines for the end of this chapter. For now, we turn to an extension of the ideas behind divisibility: modular arithmetic!

Modular Arithmetic

Modular arithmetic is something you’ve been working with since you were a child! Specifically, think about how you tell and measure time:

Using “99” on your microwave: one of the less-useful “life pro tips.”

All of these calculations involved arithmetic where our numbers “wrapped around” once they got past a certain point. This idea is the basis for modular arithmetic, which is arguably the most useful mathematical concept you’ll encounter in computer science.

To make this rigorous, let’s make a definition:

Definition 1.7.Link copied Take any two integers a,na, n , where n>0n > 0 . We define the number a%na \% n , pronounced ”aa mod nn ”, by the following algorithm:

An algorithm is a step-by-step process for solving a problem or performing a calculation! We will study algorithms in more depth later in this class; you’ll also see them everywhere in Compsci 110 / 130 / 220 / 225 / essentially, every paper you’ll see in your Compsci degree!

Note: if n<0n < 0 , we can use the same process as above to calculate a%na \% n . The only change is that we replace nn with n|n| in our steps, to compensate for the fact that nn is negative.

We call %\% the modulus operator. Most programming languages implement it as described here, though (as always) you should read the documentation for details.

To get a handle on this, we calculate a number of examples:

Example 1.8.

We also study a quick argument-based problem, to make sure that we’re comfortable with a more “abstract” way of working with the modulus operator %\% :

Claim 1.7.Link copied If a,na, n are any two integers with n>0n> 0 , the quantity a%na \% n exists and is between 00 and n1n-1 . That is: the algorithm given above to calculate %\% will never “crash” nor “run forever,” and it will always generate an output between 0 and n1n-1 .

Proof.

As always, because this is a claim about any two integers, we need to work in general. That is: we cannot pick examples for aa and nn , and instead need to consider every possible pair of values for a,na, n with n>0n > 0 .

Our algorithm had three different cases: one for when ana \geq n , one for when a<0a < 0 , and one for when 0a<n0 \leq a < n . As such, our argument will likely want three cases as well:

Because every integer aa falls into one of these three cases, we’ve proven our claim for all values of aa and all n>0n > 0 , as desired. \Box

This sort of argument (that a given algorithm “works”) is one that we will make repeatedly in this class! More generally, these are the most common sorts of arguments you’ll want to make in computer science: when working with any problems that require nontrivial algorithms or thought, you’ll often want to be able to write proofs that the process should be bug-free in theory.

Some useful notation for rounding: given any number xx , we say that x\lfloor x \rfloor is ”xx rounded down,” x\lceil x \rceil is ”xx rounded up,” and [x][x] is ”xx rounded to the nearest integer, with .5 rounding up.”

For example, π=3\lfloor \pi \rfloor = 3 , π=4\lceil \pi \rceil = 4 , [π]=3[\pi] = 3 , and [3.5]=4[3.5] = 4 .

Modular arithmetic comes up often in computer science and mathematics:

This specific cipher is easy to crack: a simple brute-force approach of trying all 26 keys one-by-one on the encrypted text would lead you to the right answer pretty quickly. (Try it out by hand or by writing a program; it’s not too bad.)

There are better ways to keep information secure, though! See courses like Compsci 110 and Maths 328 for some more modern approaches (that still use modular arithmetic at their core!)

% and Arithmetic

Question 1. What is 123456789101112131415161718191234567891011121314151617181912345678910111213141516171819^{12345678910111213141516171819} ?

If you tried to use a computer to directly expand this problem, the size of the resulting number (approximately 10102910^{10^{29}} ) would take up 1017\approx 10^{17} terabytes, i.e. several orders of magnitude in excess of the current total storage capacity of humanity.

As of 2019, according to a variety of articles + some back-of-the-envelope estimation by the coursebook’s authors. By way of comparison, the total data storage of humanity was about 10810^8 terabytes in 2008. To put that figure in modern perspective: YouTube broadcasted more than this much data in the past six months.

And yet, if you pop over to WolframAlpha and ask it this question, you’ll get the following answer in about three seconds:

So: how is WolframAlpha able to calculate this so quickly, if the precise result is so staggeringly huge?

Partly, this is because the “powers-of-ten” approximation can be made by replacing the values here with just large powers of 10: i.e. things like 12345678910111213141516171819102812345678910111213141516171819 \approx 10^{28} can make approximating things a bit easier. However, WolframAlpha also seems to know the last few digits of this number, which seems a little bit spooky: how can you calculate this without understanding the entire number?

The trick here is the modulus operation, which we can use to perform certain arithmetic tasks very quickly. To show how this is possible, we introduce the closely related idea of congruency modulo nn :

Definition 1.8.Link copied Take any three integers a,b,na,b,n . We say that aa is congruent to bb modulo nn , and write abmodna \equiv b \mod n , if aba-b is a multiple of nn .

The phrase ”aa is equivalent to bb modulo nn ” is also frequently used for this concept.

To get an idea for how this works, we calculate a number of examples here:

Example 1.9.

Congruency and our modulus operator are very closely linked:

Claim 1.8.Link copied Take any three values a,b,na,b,n such that n0n \neq 0 . Then the following two statements are equivalent:
  • a%n=b%na \% n = b \% n .
  • aba-b is a multiple of nn ; i.e. abmodna \equiv b \mod n .

In English/mathematics, we say that two statements are equivalent if they hold in precisely the same situations: i.e. whenever one is true, the other is true, and vice-versa. For example, the two statements ”nn is an even prime number” and ”n=2n = 2 ” are equivalent, as they both describe the same thing!

We reserve proving this claim for your practice problems at the end of this chapter (see exercise 7). Instead, let’s talk about how we can use this:

Claim 1.9.Link copied Suppose that a,b,c,d,na,b,c,d,n are any set of integers with n0n \neq 0 , such that a%n=b%na \% n = b \% n and c%n=d%nc \% n = d \% n .

Then we have the following properties:

  • (a+c)%n=(b+d)%n.(a+c) \% n = (b+d) \% n.
  • (ac)%n=(bd)%n.(ac) \% n = (bd) \% n.

This claim is basically just saying that we can do “arithmetic modulo nn !” That is: for numbers a,b,c,da,b,c,d , you know that if a=b,c=da=b, c=d then ac=bdac=bd and a+c=b+da+c = b+d , by just combining these equalities with the addition and multiplication operations. This claim is saying that if your values are “equal modulo nn ,” the same tricks work!

We prove this claim here:

Proof.

We need to prove our claim in general here, as we’re making a claim about any possible set of values, not a specific set of values! To do so, we let a,b,c,d,na,b,c,d,n be any set of integers such that a%n=b%na \% n = b \% n and c%n=d%nc \% n = d \% n .

If we now use Claim 1.8, we get the following: aba-b is a multiple of nn , and cdc-d is a multiple of nn . By definition, this means that we can write ab=kna-b = kn and cd=lnc-d = ln for some pair of integers k,lk, l .

By adding these equations together, we get (a+c)(b+d)=kn+ln=(k+l)n(a+c)-(b+d) = kn+ln = (k+l)n ; that is, that (a+c)(b+d)(a+c)-(b+d) is a multiple of nn . This means that, by Definition 1.8, a+cb+dmodna+c \equiv b+d \mod n ! Applying Claim 1.8 gives us (a+c)%n=(b+d)%n(a+c) \% n = (b+d) \% n , as claimed.

If we take ab=kna-b = kn and multiply both sides by cc , we get acbc=kcnac - bc = kcn ; similarly, if we take cd=lnc-d = ln and multiply by bb we get bcbd=blnbc-bd = bln . Adding these equations together gives us acbc+bcbd=kcn+bln=(kc+bl)nac-bc+bc-bd= kcn+bln = (kc+bl)n ; i.e. acbdac-bd is a multiple of nn . This means that acbdmodnac \equiv bd \mod n ! Again, applying Claim 1.8 then tells us that (ac)%n=(bd)%n(ac) \% n = (bd) \% n , as desired.

A quick corollary to Claim 1.9 is the following:

More English/mathematics word definitions! A corollary is a claim that follows quickly from the claim that just came before. That is, a corollary is a “consequence” of the earlier claim, or something that you get “for free” once you’ve proven some earlier result.

Corollary 1.1. If a%n=b%na\%n = b\% n , then for any positive integer kk , we have (ak)%n=(bk)%n(a^k) \% n = (b^k) \% n .

We leave proving this for Exercise 9; try proving this by using the multiplication property in Claim 1.9!

Instead of spending more time with this sort of abstract stuff, let’s do something practical: let’s talk about how we could solve our WolframAlpha problem! Specifically, let’s solve the following problem:

Claim 1.10.Link copied

The last digit of 213047129314213047^{129314} is 99 .

Proof.

The clever thing here is not that we can calculate this (again, if we just wanted an answer, we could plug this into WolframAlpha), but rather that we can calculate this easily! That is: we can use modular arithmetic to find this number without needing a calculator, with relatively little work overall.

To do so, just make the following observations:

In other words, our number’s last digit is 9\boxed{9} !

It’s worth noting that this trick isn’t just useful for humans, but for computers as well: you can use tricks like this to massively speed up calculations in which you only care about the number’s last few digits, and/or other pieces of partial information.

We can use this trick to basically calculate any number to any other number’s last digit! For example, the task of finding the last digit of

123456789101112131415161718191234567891011121314151617181912345678910111213141516171819^{12345678910111213141516171819}

is now something we can do very quickly:

Generalizations of this process (i.e. working modulo 100 to find the last two digits, etc) can be used to quickly find the last few digits of any number. This can be quite useful when writing computer algorithms: most of the time when working with large numbers, you only need the first few and last digits for an approximation of its size, not the entire exact value!

Other Number Systems

In the past five sections, we’ve focused on studying the integers; i.e. whole numbers. However, there are lots of problems in real life whose answers cannot be given with just integers alone. Even going back to primary school, you’ve seen and solved problems like the following:

Note: pavlovas are likely not part of a balanced breakfast.

The answers to each of these (23,3213=76 and 10100=110, respectively)\left(\frac23, \frac32 - \frac13 = \frac76\textrm{ and }\frac{10}{100} = \frac{1}{10}\textrm{, respectively}\right) are all relatively easy to calculate, and are tasks you’ve seen in school for years now! However, their solutions are not things that we can express as integers.

To work with them, we need some new sets of numbers! We start with one such set that you’re already familiar with:

Definition 1.9.Link copied A rational number is any number that we can express as a ratio xy\dfrac{x}{y} , where x,yx, y are integers and yy is nonzero. We let Q\mathbb{Q} denote the collection of all rational numbers.

Definition 1.10.Link copied Given a rational number xx , we say that xx is the numerator of xy\frac{x}{y} , and that yy is the denominator.

Example 1.10 The numbers 12\frac{1}{2} , 213,36\frac{-2}{13}, \frac{3}{6} and 07\frac{0}{7} are all rational numbers. The numerator of 36\frac{3}{6} is 3, and the denominator is 6; similarly, the numerator of 07\frac{0}{7} is 0, and the denominator is 7.

Notice that every integer xx can be written as a rational number, because x=x1x = \frac{x}{1} .

There are several operations we know how to perform on rational numbers:

Fact 1.1. If ab,cd\frac{a}{b}, \frac{c}{d} are a pair of rational numbers, then we can add and multiply them! Specifically, we say that

ab+cd=ad+bcbd,\dfrac{a}{b} + \dfrac{c}{d} = \dfrac{ad + bc}{bd}, and\text{and} abcd=acbd.\dfrac{a}{b}\cdot \dfrac{c}{d} = \dfrac{ac}{bd}.

Notice that these are both still rational numbers, as their numerator and denominators are still integers, and the denominator is nonzero because b,d0b, d \neq 0 .

With these operations in mind, we can answer our second exercise:

Answer to Exercise 1.2. In this problem, we’re asking if it is ever possible for 1x+1y\frac{1}{x} + \frac{1}{y} to be equal to 1x+y\frac{1}{x+y} .

If you’re ever given a problem like this (i.e. someone hands you an equation and asks you “are there any solutions,”) the first thing you should try to do is solve the equation! That is: try to rearrange the equation for one of your variables in terms of the other and/or some constants, and hope that this tells you something.

In this situation, we know that x,yx,y and x+yx+y must be nonzero if the fractions 1x,1y,1x+y\frac{1}{x}, \frac{1}{y}, \frac{1}{x+y} are defined. Therefore, the numbers 1x,1y\frac{1}{x}, \frac{1}{y} are rationals, and so their sum is just x+yxy\frac{x+y}{xy} as noted above.

If x+yxy=1x+y\frac{x+y}{xy} = \frac{1}{x+y} , then because x,yx,y and x+yx+y are all nonzero, we can multiply both sides by the denominators (i.e. multiply both sides by xy(x+y)xy(x+y) .) Doing so will get rid of our fractions, as

(xy)(x+y)(x+yxy)=(xy)(x+y)(1x+y)(xy)(x+y) \left(\frac{x+y}{xy}\right) = (xy)(x+y) \left(\frac{1}{x+y}\right)
(x+y)2=xy\Rightarrow \quad (x+y)^2 = xy
x2+2xy+y2=xy\Rightarrow \quad x^2+2xy + y^2 = xy
x2+xy+y2=0.\Rightarrow \quad x^2 + xy + y^2 = 0.

This is a simpler equation! In particular, if we think of xx as our variable and yy as a constant, we have a quadratic equation. In general, a quadratic equation of the form ax2+bx+c=0ax^2 + bx + c = 0 has the two solutions b±b24ac2a\frac{-b \pm \sqrt{b^2-4ac}}{2a} when b24acb^2-4ac is nonnegative, and has no real-valued solutions if b24acb^2-4ac is negative.

In this situation, the quadratic formula above tells us that a=1,b=y,c=y2a =1, b=y, c=y^2 , and thus that our solutions (if they exist) would be y±y24y22\frac{-y \pm \sqrt{y^2-4y^2}}{2} . However, we know that y24y2=3y2y^2-4y^2 = -3y^2 is always a negative number; this is because 3-3 is negative, while y2y^2 is a nonzero number squared (and thus is positive.) Therefore we have no solutions! \Box

There are other number systems that you’ve likely seen before in high school as well:

Definition 1.11.Link copied The natural numbers, denoted N\mathbb{N} , is the collection of all nonnegative integers. That is, N={0,1,2,3,4,5,}\mathbb{N} = \{0,1,2,3,4,5,\ldots\} .

Definition 1.12.Link copied The real numbers, denoted R\mathbb{R} , is the collection of all numbers that you can write out with a (possibly infinite) decimal expansion: i.e. it’s the collection of things like

So, for example, the real number contain all of the rational numbers, because you can do things like write

However, there are also real numbers that are not rationals: i.e. there are quantities out there in the world that we cannot express as a ratio of integers! We call such numbers irrational.

Observation 1.4.Link copied Notice that every real number, by definition, is either rational or irrational.

It’s a bit beyond the skills we have at the moment, but numbers like

π=3.1415926535897932384626433832795028841971693\pi = 3.1415926535897932384626433832795028841971693\ldots

and

e=2.7182818284590452353602874713526624977572470e = 2.7182818284590452353602874713526624977572470\ldots

are both irrational numbers. Later on in this class, we’ll prove that 2\sqrt{2} is also an irrational number (check out the proof by contradiction section of this book if you cannot wait!)

With that said, though, it would be a bit of a shame to not show you at least one irrational number. So, let’s close this chapter by doing this!

Claim 1.11.Link copied The number log2(3)\log_2(3) is not rational.

Proof.

We prove this claim by using the “suppose we’re wrong” structure we’ve successfully used in Claims 1.6 and 1.3. That is: what would happen if log2(3)\log_2(3) was rational?

Well: we could write log2(3)=xy\log_2(3) = \frac{x}{y} , for two integers x,yx,y . Note that because log2(3)\log_2(3) is positive (it’s greater than log2(2)=1\log_2(2) = 1 ), we can have x,y>0x, y > 0 .

Exponentiating both sides of this equation by 2 gives us 2log2(3)=2x/y2^{\log_2(3)}= 2^{x/y} . By definition, log2()\log_2(\star) and 22^\star cancel each other out, so this simplifies to 3=2x/y3 = 2^{x/y} .

Now, raising both sides to the yy -th power gives us 3y=(2x/y)y3^y = \left(2^{x/y}\right)^y . By using our known rules for exponentiation, this simplifies to 3y=2x3^y = 2^x .

On the left-hand-side, we have an odd number: it’s just yy copies of 33 multiplied together! On the right-hand-side, however, we have an even number: it’s xx copies of 2 multiplied together.

These two values cannot be equal! In other words, our initial assumption that we could write log2(3)=xy\log_2(3) = \frac{x}{y} must have been flawed, as this assumption would imply that a number can be even and odd at the same time. Therefore log2(3)\log_2(3) is irrational, as desired.

We close this chapter with some exercises for you to try out. Give these an attempt, and post your answers / attempts on Piazza!

Practice Problems

  1. (-) Let mm be even. Show that m-m is also even.
  2. Show that if aa and bb are both odd, then abab is also odd.
  3. (+) Show that if aa is an even number and kk is a positive integer, then aka^k is even.
  4. In Claim 1.3, we showed that no number is both even and odd at the same time. Prove the converse of this claim: that is, prove that every integer is either even or odd (i.e. you can’t have an integer that is both not even and not odd.)
  5. (-) Write down all of the numbers between 1 and 100 that are congruent to 2 modulo 12.
  6. How many numbers between 1 and 1000 are congruent to 0 modulo 3? How many are congruent to 0 modulo 4? Can you make a formula to count how many numbers between 1 and 1000 are congruent to 0 modulo nn , that works for any positive integer nn ?
  7. (+) Prove Claim 1.11.
  8. Take any three-digit number abcabc (where cc is the ones digit, bb is the tens digit, and aa is the hundreds digit.) Show that abcabcabcabc is never a prime number.
  9. Prove Corollary 1.1.
  10. (+) Suppose that p1,p2,pnp_1, p_2, \ldots p_n are all prime numbers. Must the number N=1+(p1p2pn)N = 1+ (p_1 \cdot p_2 \cdot \ldots \cdot p_n) be prime? Either explain why, or find a set of prime numbers such that the value NN defined above is not prime.
  11. (-) Show that n2nn^2-n is always even, for any integer nn .
  12. Show that if nn is an odd number, then n21n^2-1 is a multiple of 8.
  13. Find the last digit of 123456789012345^{67890} without using a computer or calculator.
  14. (+) Find the last digit of 9876543219876^{54321} without using a computer or calculator.
  15. (+) What is the longest English word that can be shift-ciphered into another English word?
  16. (++) Take an integer nn . If it’s odd, replace it with 3n+13n+1 ; if it’s even, replace it with n/2n/2 . Repeat this until the number is equal to 1. Will this process always eventually stop?

(from https://xkcd.com/710/)

  1. Consider the following claim: “Let ab,cd\dfrac{a}{b}, \dfrac{c}{d} be a pair of rational numbers. Suppose that ad>bcad > bc . Then we have ab>cd\dfrac{a}{b} > \dfrac{c}{d} .” Is this claim true? If it is, explain why. If it is not, find a pair of fractions that demonstrate that this claim is false.
  2. If xx is rational and yy is irrational, must x+yx+y be irrational? Either explain why or find a counterexample (i.e. find a pair of numbers x,yx,y such that xx is rational, yy is irrational, and x+yx+y is rational.)
  3. If xx and yy are both irrational numbers, must x+yx+y be irrational? Either explain why or find a counterexample (i.e. find a pair of numbers x,yx,y such that x,yx,y are irrational, but x+yx+y is rational.)
  4. (+) Show that if a,b,ca,b,c are all odd integers, then there is no rational number xx such that the equation ax2+bx+c=0ax^2+bx+c = 0 holds.
  5. (++) Is π+e\pi + e irrational?