deploy
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.1 An Introduction to Number Theory</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||||
div.sourceCode { margin: 1em 0; }
|
||||
pre.sourceCode { margin: 0; }
|
||||
@media screen {
|
||||
div.sourceCode { overflow: auto; }
|
||||
}
|
||||
@media print {
|
||||
pre > code.sourceCode { white-space: pre-wrap; }
|
||||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||||
}
|
||||
pre.numberSource code
|
||||
{ counter-reset: source-line 0; }
|
||||
pre.numberSource code > span
|
||||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||||
pre.numberSource code > span > a:first-child::before
|
||||
{ content: counter(source-line);
|
||||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||||
border: none; display: inline-block;
|
||||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||||
-khtml-user-select: none; -moz-user-select: none;
|
||||
-ms-user-select: none; user-select: none;
|
||||
padding: 0 4px; width: 4em;
|
||||
color: #aaaaaa;
|
||||
}
|
||||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||||
div.sourceCode
|
||||
{ }
|
||||
@media screen {
|
||||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||||
}
|
||||
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
||||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
||||
code span.at { color: #7d9029; } /* Attribute */
|
||||
code span.bn { color: #40a070; } /* BaseN */
|
||||
code span.bu { } /* BuiltIn */
|
||||
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
||||
code span.ch { color: #4070a0; } /* Char */
|
||||
code span.cn { color: #880000; } /* Constant */
|
||||
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
||||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
||||
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
||||
code span.dt { color: #902000; } /* DataType */
|
||||
code span.dv { color: #40a070; } /* DecVal */
|
||||
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
||||
code span.ex { } /* Extension */
|
||||
code span.fl { color: #40a070; } /* Float */
|
||||
code span.fu { color: #06287e; } /* Function */
|
||||
code span.im { } /* Import */
|
||||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
||||
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
||||
code span.op { color: #666666; } /* Operator */
|
||||
code span.ot { color: #007020; } /* Other */
|
||||
code span.pp { color: #bc7a00; } /* Preprocessor */
|
||||
code span.sc { color: #4070a0; } /* SpecialChar */
|
||||
code span.ss { color: #bb6688; } /* SpecialString */
|
||||
code span.st { color: #4070a0; } /* String */
|
||||
code span.va { color: #19177c; } /* Variable */
|
||||
code span.vs { color: #4070a0; } /* VerbatimString */
|
||||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.1 An Introduction to Number Theory</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>We’ve spent the first five chapters of this course studying programming in Python. We’ve been mainly focused on how we represent data and designing functions to operate on this data. Up to this point, the <em>ideas</em> behind the functions that we’ve written have been relatively straight-forward, and the challenge has been in implementing these ideas correctly using various programming techniques. Over the next two chapters, we are going to study algorithms where the ideas themselves will be more complex. It won’t be “obvious” how or why these algorithms work, and so to convince ourselves that these algorithms are correct, we’ll study the formal mathematics behind them.</p>
|
||||
<p>Our first large example of this is one that will take us the next two chapters to develop: the RSA cryptosystem, consisting of a pair of algorithms that are central to modern Internet security. If you haven’t heard about RSA, cryptosystems, or ever thought about security, don’t worry, we’ll be building all of these concepts from the ground up over the course of this chapter and the next. What will set this apart from the kind of work we’ve done so far is that to understand what these algorithms do and why they work, we’ll need to step away from code and into the realm of <em>number theory</em>, the branch of mathematics concerned with properties of integers.</p>
|
||||
<p>We’ll start our journey here with a few key definitions, some of which you’ve seen before defined formally in this course, and others that you might have heard about before, but not seen a formal definition.</p>
|
||||
<h2 id="divisibility-primality-and-the-greatest-common-divisor">Divisibility, primality, and the greatest common divisor</h2>
|
||||
<p>Here are our first two definitions; these are repeated from <a href="../03-logic/09-working-with-definitions.html">3.9 Working with Definitions</a>.</p>
|
||||
<div class="definition" data-terms="divides, divisible, factor, multiple">
|
||||
<p>Let <span class="math inline">\(n, d \in \Z\)</span>. We say that <span class="math inline">\(d\)</span> <strong>divides</strong> <span class="math inline">\(n\)</span> when there exists a <span class="math inline">\(k \in \Z\)</span> such that <span class="math inline">\(n = dk\)</span>. We use the notation <span class="math inline">\(d \mid n\)</span> to represent the statement “<span class="math inline">\(d\)</span> divides <span class="math inline">\(n\)</span>”.</p>
|
||||
<p>The following phrases are synonymous with “<span class="math inline">\(d\)</span> divides <span class="math inline">\(n\)</span>”:</p>
|
||||
<ul>
|
||||
<li><span class="math inline">\(n\)</span> <strong>is divisible by</strong> <span class="math inline">\(d\)</span></li>
|
||||
<li><span class="math inline">\(d\)</span> is a <strong>factor</strong> of <span class="math inline">\(n\)</span></li>
|
||||
<li><span class="math inline">\(n\)</span> is a <strong>multiple</strong> of <span class="math inline">\(d\)</span></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="definition" data-terms="prime">
|
||||
<p>Let <span class="math inline">\(p \in \Z\)</span>. We say <span class="math inline">\(p\)</span> is <strong>prime</strong> when it is greater than 1 and the only natural numbers that divide it are 1 and itself.</p>
|
||||
</div>
|
||||
<p>The next few definitions introduce and expand on the notion of common divisors between two numbers.</p>
|
||||
<div class="definition" data-terms="common divisor, greatest common divisor">
|
||||
<p>Let <span class="math inline">\(x, y, d \in \Z\)</span>. We say that <span class="math inline">\(d\)</span> is a <strong>common divisor</strong> of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> when <span class="math inline">\(d\)</span> divides <span class="math inline">\(x\)</span> and <span class="math inline">\(d\)</span> divides <span class="math inline">\(y\)</span>.</p>
|
||||
<p>We say that <span class="math inline">\(d\)</span> is the <strong>greatest common divisor</strong> of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> when it the largest number that is a common divisor of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>, or 0 when <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> are both 0.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">According to this definition, what is <span class="math inline">\(\gcd(0, n)\)</span> when <span class="math inline">\(n > 0\)</span>?</span> We can define the function <span class="math inline">\(\gcd : \Z \times \Z \to \N\)</span> as the function which takes numbers <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>, and returns their greatest common divisor.</p>
|
||||
</div>
|
||||
<p>You might wonder whether this definition makes sense in all cases: is it possible for two numbers to have no divisors in common? One of the statements we will prove later in this chapter is that <span class="math inline">\(1\)</span> divides every natural number. So at the very least, <span class="math inline">\(1\)</span> is a common divisor between any two natural numbers. There is a special case, when <span class="math inline">\(1\)</span> is the <em>only</em> positive divisor between two numbers.</p>
|
||||
<div class="definition" data-terms="coprime">
|
||||
<p>Let <span class="math inline">\(m, n \in \Z\)</span>. We say that <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span> are <strong>coprime</strong> when <span class="math inline">\(\gcd(m, n) = 1\)</span>.</p>
|
||||
</div>
|
||||
<h2 id="quotients-and-remainders">Quotients and remainders</h2>
|
||||
<p>The next definitions are introduced through a fundamental theorem in number theory, which extends the relationship of divisibility to that of remainders.</p>
|
||||
<div id="Quotient-Remainder Theorem" class="theorem">
|
||||
<p>(Quotient-Remainder Theorem) For all <span class="math inline">\(n \in \Z\)</span> and <span class="math inline">\(d \in \Z^+\)</span>, there exist <span class="math inline">\(q \in \Z\)</span> and <span class="math inline">\(r \in \N\)</span> such that <span class="math inline">\(n = qd + r\)</span> and <span class="math inline">\(0 \leq r < d\)</span>. Moreover, these <span class="math inline">\(q\)</span> and <span class="math inline">\(r\)</span> are <em>unique</em> for a given <span class="math inline">\(n\)</span> and <span class="math inline">\(d\)</span>.</p>
|
||||
<p>We say that <span class="math inline">\(q\)</span> is the <strong>quotient</strong> when <span class="math inline">\(n\)</span> is divided by <span class="math inline">\(d\)</span>, and that <span class="math inline">\(r\)</span> is the <strong>remainder</strong> when <span class="math inline">\(n\)</span> is divided by <span class="math inline">\(d\)</span>.</p>
|
||||
</div>
|
||||
<p>In Python, for given integers <code>n</code> and <code>d</code>, we can compute their quotient using <code>//</code>, their remainder using <code>%</code>, and both at the same time using the built-in function <code>divmod</code>:</p>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="op">>>></span> <span class="dv">9</span> <span class="op">//</span> <span class="dv">2</span></span>
|
||||
<span id="cb1-2"><a href="#cb1-2"></a><span class="dv">4</span></span>
|
||||
<span id="cb1-3"><a href="#cb1-3"></a><span class="op">>>></span> <span class="dv">9</span> <span class="op">%</span> <span class="dv">2</span></span>
|
||||
<span id="cb1-4"><a href="#cb1-4"></a><span class="dv">1</span></span>
|
||||
<span id="cb1-5"><a href="#cb1-5"></a><span class="op">>>></span> <span class="bu">divmod</span>(<span class="dv">9</span>, <span class="dv">2</span>)</span>
|
||||
<span id="cb1-6"><a href="#cb1-6"></a>(<span class="dv">4</span>, <span class="dv">1</span>)</span></code></pre></div>
|
||||
<h2 id="modular-arithmetic">Modular arithmetic</h2>
|
||||
<p>The final definition in this section introduces some notation that is extremely commonplace in number theory, and by extension in many areas of computer science. Often when we are dealing with relationships between numbers, divisibility is too coarse a relationship: as a predicate, it is constrained by the binary nature of its output. Instead, we often care about the <em>remainder</em> when we divide a number by another.</p>
|
||||
<div class="definition" data-terms="modular equivalence">
|
||||
<p>Let <span class="math inline">\(a, b, n \in \Z\)</span> and assume <span class="math inline">\(n \neq 0\)</span>. We say that <span class="math inline">\(a\)</span> is <strong>equivalent to <span class="math inline">\(b\)</span> modulo <span class="math inline">\(n\)</span></strong> when <span class="math inline">\(n \mid a - b\)</span>. In this case, we write <span class="math inline">\(a \equiv b \pmod n\)</span>.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">One warning: the notation <span class="math inline">\(a \equiv b \MOD n\)</span> is not exactly the same as <code>mod</code> or <code>%</code> operator you are familiar with from programming; here, both <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> could be much larger than <span class="math inline">\(n\)</span>, or even negative.</span></p>
|
||||
</div>
|
||||
<p>There are two related reasons why this notation is so useful in number theory. The first is that modular equivalence can be used to divide up numbers based on their remainders when divided by <span class="math inline">\(n\)</span>:</p>
|
||||
<div class="theorem">
|
||||
<p>Let <span class="math inline">\(a, b, n \in \Z\)</span> with <span class="math inline">\(n \neq 0\)</span>. Then <span class="math inline">\(a \equiv b \pmod n\)</span> if and only if <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> have the same remainder when divided by <span class="math inline">\(n\)</span>.<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote"> In Python, we could represent this as the expression <code>a % n == b % n</code>.</span></p>
|
||||
</div>
|
||||
<p>The second reason this is so useful is that almost all of the “standard” intuitions we have about equality transfer over this new notation as well, making it pretty easy to work with right at the very start.</p>
|
||||
<div class="theorem">
|
||||
<p>Let <span class="math inline">\(a, b, c, n \in \Z\)</span> with <span class="math inline">\(n \neq 0\)</span>. Then the following hold:</p>
|
||||
<ol type="1">
|
||||
<li><span class="math inline">\(a \equiv a \pmod n\)</span>.</li>
|
||||
<li>If <span class="math inline">\(a \equiv b \pmod n\)</span> then <span class="math inline">\(b \equiv a \pmod n\)</span>.</li>
|
||||
<li>If <span class="math inline">\(a \equiv b \pmod n\)</span> and <span class="math inline">\(b \equiv c \pmod n\)</span> then <span class="math inline">\(a \equiv c \pmod n\)</span>.</li>
|
||||
</ol>
|
||||
</div>
|
||||
<div class="theorem">
|
||||
<p>Let <span class="math inline">\(a, b, c, d, n \in \Z\)</span> with <span class="math inline">\(n \neq 0\)</span>. If <span class="math inline">\(a \equiv c \pmod n\)</span> and <span class="math inline">\(b \equiv d \pmod n\)</span>, then the following hold:</p>
|
||||
<ol type="1">
|
||||
<li><span class="math inline">\(a + b \equiv c + d \pmod n\)</span>.</li>
|
||||
<li><span class="math inline">\(a - b \equiv c - d \pmod n\)</span>.</li>
|
||||
<li><span class="math inline">\(a b \equiv c d \pmod n\)</span>.</li>
|
||||
</ol>
|
||||
</div>
|
||||
<p>Note that this second theorem shows that the familiar addition, subtraction, and multiplication operations preserve modular equivalence relationships. However, as we’ll study further in this chapter, this is <em>not</em> the case with division!</p>
|
||||
</section>
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,554 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.2 Proofs with Number Theory</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.2 Proofs with Number Theory</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>In Chapter 3, we studied how to express statements precisely using the language of predicate logic. But just as English enables us to make both true and false claims, the language of predicate logic allows for the expression of both true and false sentences. In this chapter, we will turn our attention to analyzing and communicating the truth or falsehood of these statements. You will develop the skills required to answer the following questions:</p>
|
||||
<ul>
|
||||
<li>How can you figure out if a given statement is True or False?</li>
|
||||
<li>If you know a statement is True, how can you convince others that it is True? How can you do the same if you know the statement is False instead?</li>
|
||||
<li>If someone gives you an explanation of why a statement is True, how do you know whether to believe them or not?</li>
|
||||
</ul>
|
||||
<p>These questions draw a distinction between the internal and external components of mathematical reasoning. When given a new statement, you’ll first need to figure out for yourself whether it is true (internal), and then be able to express your thought process to others (external). But even though we make a separation, these two processes are certainly connected: it is only after convincing yourself that a statement is true that you should then try to convince others. And often in the process of formalizing your intuition for others, you notice an error or gap in your reasoning that causes you to revisit your intuition—or make you question whether the statement is actually true!</p>
|
||||
<p>A <strong>mathematical proof</strong> is how we communicate ideas about the truth or falsehood of a statement to others. There are many different philosophical ideas about what constitutes a proof, but what they all have in common is that a proof is a mode of <em>communication</em>, from the person creating the proof to the person digesting it. In this course, we will focus on reading and creating our own written mathematical proofs, which is the standard proof medium in computer science.</p>
|
||||
<p>As with all forms of communication, the style and content of a proof varies depending on the audience. In this course, the audience for all of our proofs will be an average computer science student (and not your TA or instructor). As we will discuss, your audience determines how formal a proof should be (here, quite formal), and what background knowledge you can assume is understood without explanation (here, not much).</p>
|
||||
<p>However, there is even variation in the typical computer science student with experience in this area, so as much as possible in this course, we will introduce <em>new mathematical domains</em> to serve as the objects of study in our proofs.</p>
|
||||
<p>This approach has three very nice benefits: first, by building domains from the ground up, we can specify absolutely the common definitions and properties that everyone may assume and use freely in proofs; second, these domains are the theoretical foundation of many areas of computer science, and learning about them here will serve you well in many future courses; and third, learning about new domains will help develop the skill of <em>reading about a new mathematical context and understanding it</em>.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">In other words, you won’t just learn about new domains; you’ll learn <em>how</em> to learn about new domains!</span> The definitions and axioms of a new domain communicate the foundation upon which we build new proofs—in order to prove things, we need to understand the objects that we’re talking about first.</p>
|
||||
<h2 id="first-examples">First examples</h2>
|
||||
<p>We’re going to start out our exploration of proofs by studying a few simple statements. Our first foray into domain exploration will be into number theory, which you can think of as taking a type of entity with which we are quite familiar, and formalizing definitions and pushing the boundaries of what we actually know about these <em>numbers</em> that we use every day.</p>
|
||||
<p>You may find our first few examples a bit on the easy side, which is fine. We are using them not so much for their ability to generate mathematical insight, but rather to model both the <em>thinking</em> and the <em>writing</em> that would go into approaching a problem.</p>
|
||||
<p>Each example in this section is divided into three or four parts:</p>
|
||||
<ol type="1">
|
||||
<li>The statement that we want to prove or disprove. Sometimes, we’ll specify whether to prove or disprove it, and other times deciding whether the statement is true or false is part of the exercise.</li>
|
||||
<li>A translation of the statement into predicate logic. This step often provides insight into the <em>logical structure</em> of the statement that we are considering, which in turn informs the structure and techniques that we will use in our proofs.</li>
|
||||
<li>A discussion to try to gain some intuition about why the statement is true. You’ll tend to see that these are written very informally, as if we are talking to a friend on a whiteboard. The discussion usually will reveal the mathematical insight that forms the content of a proof. <strong>This is often the hardest part of developing a proof, so please don’t skip these sections!</strong></li>
|
||||
<li>A formal proof. This is meant to be a standalone piece of writing, the “final product” of our earlier work. Depending on the depth of the discussion, the formal proof might end up being almost mechanical – a matter of formalizing our intuition.</li>
|
||||
</ol>
|
||||
<p>With this in mind, let’s dive right in!</p>
|
||||
<div class="example">
|
||||
<p>Prove that <span class="math inline">\(23 \mid 115\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>We will <em>expand</em> the definition of divisibility to rewrite this statement in terms of simpler operations: <span class="math display">\[\exists k \in \Z,~ 115 = 23k.\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>We just need to divide 115 by 23, right?</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(k = 5\)</span>.</p>
|
||||
<p>Then <span class="math inline">\(115 = 23 \cdot 5 = 23 \cdot k\)</span>.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> We typically signal the end of a proof by writing a black square ◼ in the bottom-right corner.</span></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>We can draw from this example a more general technique for structuring our existence proofs. A statement of the form <span class="math inline">\(\exists x \in S,~P(x)\)</span> is True when at least one element of <span class="math inline">\(S\)</span> satisfies <span class="math inline">\(P\)</span> (hence our use of <code>any</code> in Python). The easiest way to convince someone that this is True is to actually find the concrete element that satisfies <span class="math inline">\(P\)</span>, and then show that it does.<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote"> Of course, this is <em>not</em> the only proof technique used for existence proofs. You’ll study more sophisticated ways of doing such proofs in future courses.</span> This is so natural a strategy that it should not be surprising that there is a “standard proof format” when dealing with such statements.</p>
|
||||
<div class="framed">
|
||||
<p><strong>A typical proof of an existential.</strong></p>
|
||||
<p>Given statement to prove: <span class="math inline">\(\exists x \in S,~P(x)\)</span>.</p>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(x = \_\_\_\_\_\_\_\)</span>.</p>
|
||||
<p>[Proof that <span class="math inline">\(P(\_\_\_\_\_\_\_)\)</span> is True.]</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Note that the two blanks represent the same element of <span class="math inline">\(S\)</span>, which <em>you</em> get to choose as a prover. Thus existence proofs usually come down to <em>finding</em> a correct element of the domain which satisfy the required properties.</p>
|
||||
<p>Here is another example which uses the same idea, but with two existentially-quantified variables.</p>
|
||||
<div class="example">
|
||||
<p>Prove that there exists an integer that divides 104.</p>
|
||||
<div class="translation">
|
||||
<p>There is the key phrase “there exists” right in the problem statement, so we could write <span class="math inline">\(\exists a \in \Z,~a \mid 104\)</span>. We can once again expand the definition of divisibility to write:<label for="sn-3" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-3" class="margin-toggle"/><span class="sidenote">We use the abbreviated form for two quantifications of the same type.</span> <span class="math display">\[\exists a, k \in \Z,~104 = ak.\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>Basically, we need to pick a pair of divisors of 104. Since this is an existential proof and we get to pick both <span class="math inline">\(a\)</span> and <span class="math inline">\(k\)</span>, any pair of divisors will work.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(a = -2\)</span> and let <span class="math inline">\(k = -52\)</span>.</p>
|
||||
<p>Then <span class="math inline">\(104 = ak\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>The previous example is the first one that had multiple quantifiers. In our proof, we had to give explicit values for both <span class="math inline">\(a\)</span> and <span class="math inline">\(k\)</span> to show that the statement held. Just as how a <em>sentence</em> in predicate logic must have all its variables quantified, a <em>mathematical proof</em> must introduce all variables contained in the sentence being proven.</p>
|
||||
<h3 id="alternating-quantifiers-revisited">Alternating quantifiers, revisited</h3>
|
||||
<p>In the Chapter 3, we saw how changing the order of an existential and universal quantifier changed the meaning of a statement. Now, we’ll study how the order of quantifiers changes how we can introduce variables in a proof.</p>
|
||||
<div class="example">
|
||||
<p>Prove that all integers are divisible by <span class="math inline">\(1\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>The statement contains a universal quantification: <span class="math inline">\(\forall n \in \Z,~1 \mid n\)</span>. We can unpack the definition of divisibility to <span class="math display">\[\forall n \in \Z,~\exists k \in \Z,~n = 1 \cdot k.\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>The final equation in the fully-expanded form of the statement is straightforward, and is valid when <span class="math inline">\(k\)</span> equals <span class="math inline">\(n\)</span>. But how should I introduce these variables? Answer: <em>in the same order they are quantified in the statement.</em></p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(n \in \Z\)</span>. Let <span class="math inline">\(k = n\)</span>.</p>
|
||||
<p>Then <span class="math inline">\(n = 1 \cdot n = 1 \cdot k\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>This proof is quite short, but introduces a few new elements. First, it introduced a variable <span class="math inline">\(n\)</span> that could represent any real number. Unlike the previous existence proofs, when we introduced this variable <span class="math inline">\(n\)</span> we did not specify a concrete value like <span class="math inline">\(10\)</span>, but rather said that <span class="math inline">\(n\)</span> was an arbitrary real number by writing ``Let <span class="math inline">\(n \in \Z\)</span>.<label for="sn-4" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-4" class="margin-toggle"/><span class="sidenote">You might notice that we use the same word “let” to introduce both existentially- and universally-quantified variables. However, you should always be able to tell how the variable is quantified based on whether it is given a concrete value or an “arbitrary” value in the proof.</span></p>
|
||||
<div class="framed">
|
||||
<p><strong>A typical proof of a universal.</strong></p>
|
||||
<p>Given statement to prove: <span class="math inline">\(\forall x \in S,~P(x)\)</span>.</p>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(x \in S\)</span>. (That is, let <span class="math inline">\(x\)</span> be an arbitrary element of <span class="math inline">\(S\)</span>.)</p>
|
||||
<p>[Proof that <span class="math inline">\(P(x)\)</span> is True].</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>The other interesting element of this proof was that it contained an existentially-quantified variable <span class="math inline">\(k\)</span> after the <span class="math inline">\(\forall n \in \Z\)</span>. We used an extremely important tool at our disposal when it comes to proofs with multiple quantifiers: <strong>any existentially-quantified variable can be assigned a value that depends on the variables defined before it.</strong></p>
|
||||
<p>In our proof, we first defined <span class="math inline">\(n\)</span> to be an arbitrary integer. Immediately after this, we wanted to show that for this <span class="math inline">\(n\)</span>, <span class="math inline">\(\exists k \in \Z,~ n = 1 \cdot k\)</span>. And to prove this, we needed a value for <span class="math inline">\(k\)</span>—a “let” statement. Because we define <span class="math inline">\(k\)</span> after having defined <span class="math inline">\(n\)</span>, we can use <span class="math inline">\(n\)</span> in the definition of <span class="math inline">\(k\)</span> and say “Let <span class="math inline">\(k = n\)</span>.” It may be helpful to think about the analogous process in programming. We first initialize a variable <span class="math inline">\(n\)</span>, and then define a new variable <span class="math inline">\(k\)</span> that is assigned the value of <span class="math inline">\(n\)</span>.</p>
|
||||
<p>Even though this may seem obvious, one important thing to note is that the <em>order of variables in the statement determines the order in which the variables must be introduced in the proof</em>, and hence which variables can depend on which other variables. For example, consider the following erroneous “proof.”</p>
|
||||
<div class="example">
|
||||
<p>(Wrong!) Prove that <span class="math inline">\(\exists k \in \Z,~\forall n \in \Z,~n = 1 \cdot k.\)</span></p>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(k = n\)</span>. Let <span class="math inline">\(n \in \Z\)</span>.</p>
|
||||
<p>Then <span class="math inline">\(n = 1 \cdot k\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>This proof may look very similar to the previous one, but it contains one crucial difference. The very first sentence, “Let <span class="math inline">\(k = n\)</span>,” is invalid: at that point, <span class="math inline">\(n\)</span> has not yet been defined!<label for="sn-5" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-5" class="margin-toggle"/><span class="sidenote"> This is analagous to a <code>NameError</code> in Python.</span> This is the result of having switched around the order of the quantifiers, which forces <span class="math inline">\(k\)</span> to be defined independently of whatever <span class="math inline">\(n\)</span> is chosen.</p>
|
||||
<p>Note: don’t assume that just because <em>one</em> proof is invalid, that <em>all</em> proofs of this statement are invalid! We cannot conclude that this statement is False just because we found one proof that didn’t work.<label for="sn-6" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-6" class="margin-toggle"/><span class="sidenote">A meta way of looking at this: a statement is True when <em>there exists</em> a correct proof of it.</span> That said, this statement is indeed False, and we’ll see later on in this chapter how to prove that a statement is False instead of True.</p>
|
||||
<h2 id="proofs-involving-implications">Proofs involving implications</h2>
|
||||
<p>Let’s look at one new example.</p>
|
||||
<div class="example">
|
||||
<p>Prove that for all integers <span class="math inline">\(x\)</span>, if <span class="math inline">\(x\)</span> divides <span class="math inline">\((x + 5)\)</span>, then <span class="math inline">\(x\)</span> also divides <span class="math inline">\(5\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>There is both a universal quantification and implication in this statement:<label for="sn-7" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-7" class="margin-toggle"/><span class="sidenote">As we noted back in Chapter 3, the “universal + implication” form is the most common form of statement we encounter.</span> <span class="math display">\[\forall x \in \Z,~ x \mid (x + 5) \Rightarrow x \mid 5.\]</span> When we unpack the definition of divisibility, we need to be careful about how the quantifiers are grouped: <span class="math display">\[\forall x \in \Z,~ \big( \exists k_1 \in \Z,~ x + 5 = k_1x \big) \Rightarrow \big( \exists k_2 \in \Z,~ 5 = k_2x \big).\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>I need to prove that if <span class="math inline">\(x\)</span> divides <span class="math inline">\(x + 5\)</span>, then it also divides 5. To prove this, I’m going to <em>assume</em> that <span class="math inline">\(x\)</span> divides <span class="math inline">\(x + 5\)</span>, and I need to <em>prove</em> that <span class="math inline">\(x\)</span> divides 5.</p>
|
||||
<p>Since <span class="math inline">\(x\)</span> is divisible by <span class="math inline">\(x\)</span>, I should be able to subtract it from <span class="math inline">\(x + 5\)</span> and keep the result a multiple of <span class="math inline">\(x\)</span>. Can I prove that using the definition of divisibility? I basically need to “turn” the equation <span class="math inline">\(x + 5 = k_1x\)</span> into the equation <span class="math inline">\(5 = k_2x\)</span>.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(x\)</span> be an arbitrary integer. <em>Assume</em> that <span class="math inline">\(x \mid (x + 5)\)</span>, i.e., that there exists <span class="math inline">\(k_1 \in \Z\)</span> such that <span class="math inline">\(x + 5 = k_1x\)</span>. We want to prove that there exists <span class="math inline">\(k_2 \in \Z\)</span> such that <span class="math inline">\(5 = k_2x\)</span>.</p>
|
||||
<p>Let <span class="math inline">\(k_2 = k_1 - 1\)</span>.</p>
|
||||
<p>Then we can calculate: <span class="math display">\[\begin{align*}
|
||||
k_2x &= (k_1 - 1)x \\
|
||||
&= k_1 x - x \\
|
||||
&= (x + 5) - x \tag{we assumed $x + 5 = k_1 x$}\\
|
||||
&= 5
|
||||
\end{align*}\]</span></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Whew, that was a bit longer than the proofs we’ve already done. There were a lot of new elements that we introduced here, so let’s break them down:</p>
|
||||
<ul>
|
||||
<li><p>After introducing <span class="math inline">\(x\)</span>, we wanted to prove the <em>implication</em> <span class="math inline">\(x \mid (x + 5) \Rightarrow x \mid 5\)</span>. To prove an implication, we needed to assume that the hypothesis was True, and then prove that the conclusion is also True. In our proof, we wrote “<strong>Assume</strong> <span class="math inline">\(x \mid (x + 5)\)</span>.”</p>
|
||||
<p>This is <em>not</em> a claim that <span class="math inline">\(x \mid (x + 5)\)</span> is True; rather, it is a way to consider what would happen <em>if</em> <span class="math inline">\(x \mid (x + 5)\)</span> were True. The goal for the rest of the proof was to prove that <span class="math inline">\(x \mid 5\)</span>.</p>
|
||||
<p>This proof structure is common when proving an implication:</p>
|
||||
<div class="framed">
|
||||
<p><strong>A typical proof of an implication (direct).</strong></p>
|
||||
<p>Given statement to prove: <span class="math inline">\(p \Rightarrow q\)</span>.</p>
|
||||
<div class="proof">
|
||||
<p>Assume <span class="math inline">\(p\)</span>.</p>
|
||||
<p>[Proof that <span class="math inline">\(q\)</span> is True.]</p>
|
||||
</div>
|
||||
</div></li>
|
||||
<li><p>When we assumed that <span class="math inline">\(x \mid (x + 5)\)</span>, what this really did was introduce a new variable <span class="math inline">\(k_1 \in \Z\)</span> from the definition of divisibility. This might seem a little odd, but take a moment to think about what this means in English. We assumed that <span class="math inline">\(x\)</span> divides <span class="math inline">\(x + 5\)</span>, which (by definition) is the same as assuming that there exists an integer <span class="math inline">\(k_1\)</span> such that <span class="math inline">\(x + 5 = k_1x\)</span>. Given that such a number exists, we can give it a name and refer to it in the rest of our proof.<label for="sn-8" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-8" class="margin-toggle"/><span class="sidenote">In other words, we introduced a variable into the proof through an <em>assumption</em> we made.</span></p></li>
|
||||
</ul>
|
||||
<h3 id="generalizing-our-example">Generalizing our example</h3>
|
||||
<p>One of the most important meta-techniques in mathematical proof is that of <strong>generalization</strong>: taking a true statement (and a proof of the statement), and then replacing a concrete value in the statement with a universally quantified variable. For example, consider the statement from the previous example, <span class="math inline">\(\forall x \in \Z,~ x \mid (x + 5) \Rightarrow x \mid 5\)</span>. It doesn’t seem like the “<span class="math inline">\(5\)</span>” serves any special purpose; it is highly likely that it could be replaced by another number like <span class="math inline">\(165\)</span>, and the statement would still hold.<label for="sn-9" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-9" class="margin-toggle"/><span class="sidenote">Concretely, consider the statement <span class="math inline">\(\forall x \in \Z,~ x \mid (x + 165) \Rightarrow x \mid 165\)</span>, which is at least as plausible as the original statement with <span class="math inline">\(5\)</span>’s.</span></p>
|
||||
<p>But rather than replace the <span class="math inline">\(5\)</span> with another concrete number and then re-proving the statement, we will instead replace it with a universally-quantified variable, and prove the corresponding statement. This way, we will know that in fact we could replace the <span class="math inline">\(5\)</span> with <em>any</em> integer and the statement would still hold.</p>
|
||||
<div class="example">
|
||||
<p>Prove that for all <span class="math inline">\(d \in \Z\)</span>, and for all <span class="math inline">\(x \in \Z\)</span>, if <span class="math inline">\(x\)</span> divides <span class="math inline">\((x + d)\)</span>, then <span class="math inline">\(x\)</span> also divides <span class="math inline">\(d\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>This has basically the same translation as last time, except now we have an extra variable: <span class="math display">\[\forall d,x \in \Z,~ \big( \exists k_1 \in \Z,~ x + d = k_1x \big) \Rightarrow \big( \exists k_2 \in \Z,~ d = k_2x \big).\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>I should be able to use the same set of calculations as last time.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(d\)</span> and <span class="math inline">\(x\)</span> be arbitrary integers. <em>Assume</em> that <span class="math inline">\(x \mid (x + d)\)</span>, i.e., there exists <span class="math inline">\(k_1 \in \Z\)</span> such that <span class="math inline">\(x + d = k_1x\)</span>. We want to prove that there exists <span class="math inline">\(k_2 \in \Z\)</span> such that <span class="math inline">\(d = k_2x\)</span>.</p>
|
||||
<p>Let <span class="math inline">\(k_2 = k_1 - 1\)</span>.</p>
|
||||
<p>Then we can calculate: <span class="math display">\[\begin{align*}
|
||||
k_2x &= (k_1 - 1)x \\
|
||||
&= k_1 x - x \\
|
||||
&= (x + d) - x \\
|
||||
&= d
|
||||
\end{align*}\]</span></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>This proof is basically the same as the previous one: we have simply swapped out all of the <span class="math inline">\(5\)</span>’s with <span class="math inline">\(d\)</span>’s. We say that the proof <em>did not depend on the value <span class="math inline">\(5\)</span></em>, meaning there was no place that we used some special property of <span class="math inline">\(5\)</span>, where we could have used a generic integer instead. We can also say that the original statement and proof <em>generalize</em> to this second version.</p>
|
||||
<p>Why does generalization matter? By generalizing the previous statement from being about the number <span class="math inline">\(5\)</span> to an arbitrary integer, we have essentially gone from one statement being true to an infinite number of statements being true. The more general the statement, the more useful it becomes. We care about exponent laws like <span class="math inline">\(a^b \cdot a^c = a^{b + c}\)</span> precisely because they apply to every possible number; regardless of what our concrete calculation is, we know we can use this law in our calculations.</p>
|
||||
<!-- ## False statements and disproofs
|
||||
|
||||
Suppose we have a friend who is trying to convince us that a certain
|
||||
statement $X$ is false. If they tell you that statement $X$ is false
|
||||
because they tried really hard to come up with a proof of it and failed,
|
||||
you might believe them, or you might wonder if maybe they just missed a
|
||||
crucial idea leading to a correct proof.^[Maybe they skipped all their CSC110 classes.] An absence of proof is not
|
||||
enough to convince us that the statement is false.
|
||||
|
||||
Instead, we must see a
|
||||
**disproof**, which is simply a proof that the *negation* of the
|
||||
statement is true.^[In other words, if we can prove that $\NOT X$ is true, then $X$ must be false.] For this section, we’ll be using the simplification
|
||||
rules from the first chapter to make negations of statements easier to
|
||||
work with.
|
||||
|
||||
Here are two examples: the first one is quite simple, and is used to
|
||||
introduce the basic idea. The second is more subtle, and really requires
|
||||
good understanding of how we manipulate a statement to get a simple form
|
||||
for its negation.
|
||||
|
||||
<div class="example">
|
||||
Disprove the following statement: every natural number divides 360.
|
||||
|
||||
<div class="translation">
|
||||
This statement can be written as $\forall n \in \N,~n \mid 360$.
|
||||
However, we want to prove that it is false, so we really need to study
|
||||
its negation.
|
||||
\begin{align*}
|
||||
\NOT \big(\forall n \in \N,~n \mid 360 \big) \\
|
||||
\exists n \in \N,~ n \NDIV 360
|
||||
\end{align*}
|
||||
</div>
|
||||
<div class="discussion">
|
||||
The original statement is obviously not true: the number 7 doesn’t
|
||||
divide 360, for instance. Is that a proof? We wrote the negation of the
|
||||
statement in symbolic form above, and if we translate it back into
|
||||
English, we get "there exists a natural number which does not divide
|
||||
360." So, yes. That’s enough for a proof.
|
||||
</div>
|
||||
<div class="proof">
|
||||
Let $n = 7$.
|
||||
|
||||
Then $n \NDIV 360$, since $\frac{360}{7}$ is not an integer.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
When we want disprove a universally-quantified statement ("every element
|
||||
of $S$ satisfies predicate $P$"), the negation of that statement becomes
|
||||
an existentially-quantified one ("there exists an element of $S$ that
|
||||
doesn’t satisfy predicate $P$"). Since proofs of existential
|
||||
quantification involve just finding one value, the disproof of the
|
||||
original statement involves finding such a value which causes the
|
||||
predicate to be false (or alternatively, causes the negation of the
|
||||
predicate to be true). We call this value a **counterexample** for the
|
||||
original statement. In the previous example, we would say that 7 is a counterexample of the given statement.
|
||||
|
||||
<div framed>
|
||||
**A typical disproof of a universal (counterexample).**
|
||||
|
||||
Given statement to *disprove*: $\forall x \in S,~P(x)$.
|
||||
|
||||
<div class="proof">
|
||||
We prove the negation, $\exists x \in S,~\NOT P(x)$.
|
||||
Let $x=$ `_______`.
|
||||
|
||||
[Proof that $\NOT P$(`_______`) is True.]
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Now let's look at at a more complex disproof.
|
||||
|
||||
<div class="example">
|
||||
Disprove the following claim: for all natural numbers $a$ and $b$, there
|
||||
exists a natural number $c$ which is less than $a + b$, and greater than
|
||||
both $a$ and $b$, such that $c$ is divisible by $a$ or by $b$.
|
||||
|
||||
<div class="translation">
|
||||
The original statement can be translated as follows. We’ve underlined
|
||||
the four different propositions which are joined with **AND** operators to make
|
||||
them stand out.
|
||||
$$\forall a, b \in \N,~\exists c \in \N,~
|
||||
\underline{c < a + b} \AND
|
||||
\underline{c > a} \AND
|
||||
\underline{c > b} \AND
|
||||
\underline{(a \mid c \OR b \mid c)}.$$
|
||||
|
||||
We’ll derive the negation step by step, though once you get comfortable
|
||||
with the negation rules, you’ll be able to handle even complex formulas
|
||||
like this one quite quickly.
|
||||
\begin{align*}
|
||||
\NOT \Big(
|
||||
\forall a, b \in \N,~\exists c \in \N,~
|
||||
\underline{c < a + b} \AND
|
||||
\underline{c > a} \AND
|
||||
\underline{c > b} \AND
|
||||
\underline{(a \mid c \OR b \mid c)} \Big) \\
|
||||
\exists a, b \in \N,~ \NOT \Big(
|
||||
\exists c \in \N,~
|
||||
\underline{c < a + b} \AND
|
||||
\underline{c > a} \AND
|
||||
\underline{c > b} \AND
|
||||
\underline{(a \mid c \OR b \mid c)} \Big) \\
|
||||
\exists a, b \in \N,~ \forall c \in \N,~
|
||||
\NOT \Big(
|
||||
\underline{c < a + b} \AND
|
||||
\underline{c > a} \AND
|
||||
\underline{c > b} \AND
|
||||
\underline{(a \mid c \OR b \mid c)} \Big) \\
|
||||
\exists a, b \in \N,~ \forall c \in \N,~
|
||||
\underline{c \geq a + b} \OR
|
||||
\underline{c \leq a} \OR
|
||||
\underline{c \leq b} \OR
|
||||
\underline{\Big(\NOT (a \mid c \OR b \mid c) \Big)} \\
|
||||
\exists a, b \in \N,~ \forall c \in \N,~
|
||||
\underline{c \geq a + b} \OR
|
||||
\underline{c \leq a} \OR
|
||||
\underline{c \leq b} \OR
|
||||
\underline{(a \NDIV c \AND b \NDIV c)}\end{align*}
|
||||
</div>
|
||||
<div class="discussion">
|
||||
That symbolic negation involved quite a bit of work. Let’s make sure we
|
||||
can translate the final result back into English: there exist natural
|
||||
numbers $a$ and $b$ such that for all natural numbers $c$,
|
||||
$c \geq a + b$ or $c \leq a$ or $c \leq b$ or neither $a$ nor $b$ divide
|
||||
$c$. Hopefully this example illustrates the power of predicate logic: by
|
||||
first translating the original statement into symbolic logic, we were
|
||||
able to obtain a negation by applying some standard manipulation rules
|
||||
and then translating the resulting statement back into English. For a
|
||||
statement as complex as this one, it is usually easier to do this than
|
||||
to try to intuit what the English negation of the original is, at
|
||||
least when you’re first starting out.
|
||||
|
||||
Okay, so how do we prove the negation? The existential quantifier tells
|
||||
us we get to pick $a$ and $b$. Let’s think simple: what if $a$ and $b$
|
||||
are both 2? Then $a + b = 4$. If $c \geq 4$, the first clause in the OR
|
||||
is satisfied, and if $c \leq 2$, the second and third clauses are
|
||||
satisfied. So we only need to worry about when $c$ is 3, because in this
|
||||
case the only clause that could possibly be satisfied is the last one,
|
||||
$a \NDIV c \AND b \NDIV c$. Luckily, $a$ and $b$ are both 2, and 2
|
||||
doesn’t divide 3, so it seems like we’re good in this case as well.
|
||||
|
||||
It was particularly helpful that we chose such small values for $a$ and
|
||||
$b$, so that there weren’t a lot of numbers in between them and their
|
||||
sum to care about. As you do your own proofs of existentially-quantified
|
||||
statements, remember that you have the power to pick values for these
|
||||
variables!
|
||||
</div>
|
||||
<div class="proof">
|
||||
Let $a = 2$ and $b = 2$, and let $c \in \N$. We now need to prove that
|
||||
$$c \geq a + b \OR c \leq a \OR c \leq b \OR (a \NDIV c \AND b \NDIV c).$$
|
||||
|
||||
Substituting in the values for $a$ and $b$, this gets simplified to:
|
||||
\begin{align*}
|
||||
c \geq 4 \OR c \leq 2 \OR 2 \NDIV c \tag{$*$}
|
||||
\end{align*}
|
||||
|
||||
To prove an OR, we only need one of the three parts to be true, and
|
||||
different ones can be true for different values of $c$.
|
||||
|
||||
However, precisely which part is true depends on the value of $c$. For
|
||||
example, we can’t say that for an *arbitrary* value of $c$, that
|
||||
$c \geq 4$. So we’ll split up the remainder of the proof into three
|
||||
cases for the values for $c$: numbers $\geq 4$, numbers $\leq 2$, and
|
||||
the single value 3.
|
||||
|
||||
**Case 1**. We will *assume* that $c \geq 4$, and prove the
|
||||
statement $(*)$ is true.
|
||||
|
||||
In this case, the first part of the OR in $(*)$ is true (this is exactly what we've assumed).
|
||||
|
||||
**Case 2**. We will *assume* that $c \leq 2$, and prove the
|
||||
statement $(*)$ is true.
|
||||
|
||||
In this case, the second part of the OR in $(*)$ is true (this is exactly what we've assumed).
|
||||
|
||||
**Case 3**. We will *assume* that $c = 3$, and prove the statement
|
||||
$(*)$ is true.
|
||||
|
||||
This case is the trickiest, because unlike the others, our assumption
|
||||
that $c = 3$ is not verbatim one of the parts of $(*)$. However, we note
|
||||
that $2 \NDIV 3$, and so the third part of the OR is satisfied.
|
||||
|
||||
Since in all possible cases statement $(*)$ is true, we conclude that
|
||||
this statement is always true.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
## Proof by cases
|
||||
|
||||
The previous proof illustrated a new proof technique known as **proof by
|
||||
cases**. Remember that for a universal proof, we typically let a
|
||||
variable be an arbitrary element of the domain, and then make an
|
||||
argument in the proof body to prove our goal statement. However, even
|
||||
when the goal statement is true for all elements of the domain, it isn’t
|
||||
always easy to construct a single argument that works for all of those elements!
|
||||
Sometimes, different arguments are required for different elements.
|
||||
In this case, we divide the domain into different parts, and
|
||||
then write a separate argument for each part.
|
||||
|
||||
A bit more formally, we pick a set of unary predicates $P_1$, $P_2$, …,
|
||||
$P_k$ (for some positive integer $k$), such that for every element $x$
|
||||
in the domain, $x$ satisfies at least one of the predicates (we say that
|
||||
these predicates are *exhaustive*).
|
||||
You should think of these predicates as describing how we divide up the domain;
|
||||
in the previous example, the predicates were:
|
||||
$$P_1(c): c \leq 2, \qquad P_2(c): c \geq 4, \qquad P_3(c): c = 3.$$
|
||||
|
||||
Then, we divide the proof body into cases, where in each case we
|
||||
*assume* that one of the predicates is True, and use that assumption to construct a
|
||||
proof that specifically works under that assumption.^[
|
||||
Recall that there's an equivalence between predicates and sets.
|
||||
Another way of looking at a proof by cases is that we divide the domain into subsets $S_1, S_2, \dots S_k$, and then prove the desired statement separately for each of these subsets.
|
||||
]
|
||||
|
||||
<div framed>
|
||||
**A typical proof by cases.**
|
||||
|
||||
Given statement to prove: $\forall x \in S, P(x).$
|
||||
Pick a set of exhaustive predicates $P_1, \dots, P_k$ of $S$.
|
||||
|
||||
<div class="proof">
|
||||
Let $x \in S$.
|
||||
We will use a proof by cases.
|
||||
|
||||
**Case 1**. *Assume* $P_1(x)$ is True.
|
||||
|
||||
[Proof that $P(x)$ is True, assuming $P_1(x)$.]
|
||||
|
||||
**Case 2**. *Assume* $P_2(x)$ is True.
|
||||
|
||||
[Proof that $P(x)$ is True, assuming $P_2(x)$.]
|
||||
|
||||
$\vdots$
|
||||
|
||||
**Case $k$**. *Assume* $P_k(x)$ is True.
|
||||
|
||||
[Proof that $P(x)$ is True, assuming $P_k(x)$.]
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Proof by cases is a very versatile proof technique, since it allows the
|
||||
combining of simpler proofs together to form a whole proof. Often it is
|
||||
easier to prove a property about some (or even most) elements of the
|
||||
domain than it is to prove that same property about all the elements.
|
||||
But do keep in mind that if you can find a *simple* proof which works
|
||||
for all elements of the domain, that’s generally preferable than
|
||||
combining multiple proofs together in a proof by cases.
|
||||
|
||||
To see one natural use of proof by cases in number theory, we introduce the following theorem, which formalizes our intuitions about another familiar term: remainders.
|
||||
|
||||
<div theorem id="theorem:quotient_remainder">
|
||||
(Quotient-Remainder Theorem)
|
||||
For all $n \in \Z$ and $d \in \Z$, if $d \neq 0$ then
|
||||
there exist $q, r \in \Z$ such that $n = qd + r$ and $0 \leq r < |d|$.
|
||||
Moreover, these $q$ and $r$ are *unique* (they are determined entirely by the values of $n$ and $d$).
|
||||
</div>
|
||||
|
||||
<div definition data-terms="quotient,remainder">
|
||||
Let $n, d, q, r$ be the variables in the previous theorem.
|
||||
We say that $q$ and $r$ are the **quotient** and **remainder**, respectively, when $n$ is divided by $d$.
|
||||
</div>
|
||||
|
||||
The reason this theorem is powerful is that it tells us that for any non-zero divisor $d \in \Z$, we can separate all possible integers into $d$ different groups, corresponding to their possible remainders (between $0$ and $|d|-1$) when divided by $d$.
|
||||
Let's see this how to use this fact to perform a proof by cases.
|
||||
|
||||
<div class="example">
|
||||
Prove that for all integers $x$, $2 \mid x^2 + 3x$.
|
||||
|
||||
<div class="translation">
|
||||
Using the divisibility predicate: $\forall x \in \Z,~ 2 \mid x^2 + 3x$.
|
||||
Or expanding the definition of divisibility:
|
||||
$\forall x \in \Z,~ \exists k \in \Z,~ x^2 + 3x = 2k$.
|
||||
</div>
|
||||
|
||||
<div class="discussion">
|
||||
We want to "factor out a $2$" from the expression $x^2 + 3x$, but this only works if $x$ is even.
|
||||
If $x$ is odd, though, then both $x^2$ and $3x$ will be odd, and adding two odd numbers together produces an even number.
|
||||
|
||||
But how do we "know" that every number has to be either even or odd?
|
||||
And how can we formalize the algebraic operations of "factoring out a $2$" or "adding two odd numbers together"?
|
||||
This is where the Quotient-Remainder Theorem comes in.
|
||||
</div>
|
||||
|
||||
<div class="proof">
|
||||
Let $x \in \Z$.
|
||||
By the Quotient-Remainder Theorem, we know that when $x$ is divided by $2$, the two possible remainders are $0$ and $1$.
|
||||
We will divide up the proof into two cases based on these remainders.
|
||||
|
||||
**Case 1**: assume the remainder when $x$ is divided by $2$ is $0$.
|
||||
That is, we assume there exists $q \in \Z$ such that $x = 2q + 0$.
|
||||
Let $k = 2q^2 + 3q$. We will show that $x^2 + 3x = 2k$.
|
||||
|
||||
We have:
|
||||
\begin{align*}
|
||||
x^2 + 3x &= (2q)^2 + 3(2q) \\
|
||||
&= 4q^2 + 6q \\
|
||||
&= 2(2q^2 + 3q) \\
|
||||
&= 2k
|
||||
\end{align*}
|
||||
|
||||
**Case 2**: assume the remainder when $x$ is divided by 2 is $1$.
|
||||
That is, we assume there exists $q \in \Z$ such that $x = 2q + 1$.
|
||||
Let $k = 2q^2 + 5q + 2$. We will show that $x^2 + 3x = 2k$.
|
||||
|
||||
We have:
|
||||
\begin{align*}
|
||||
x^2 + 3x &= (2q+1)^2 + 3(2q+1) \\
|
||||
&= 4q^2 + 4q + 1 + 6q + 3 \\
|
||||
&= 2(2q^2 + 5q + 2) \\
|
||||
&= 2k
|
||||
\end{align*}
|
||||
</div>
|
||||
</div> -->
|
||||
</section>
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,229 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.3 Proofs and Algorithms I: Primality Testing</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||||
div.sourceCode { margin: 1em 0; }
|
||||
pre.sourceCode { margin: 0; }
|
||||
@media screen {
|
||||
div.sourceCode { overflow: auto; }
|
||||
}
|
||||
@media print {
|
||||
pre > code.sourceCode { white-space: pre-wrap; }
|
||||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||||
}
|
||||
pre.numberSource code
|
||||
{ counter-reset: source-line 0; }
|
||||
pre.numberSource code > span
|
||||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||||
pre.numberSource code > span > a:first-child::before
|
||||
{ content: counter(source-line);
|
||||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||||
border: none; display: inline-block;
|
||||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||||
-khtml-user-select: none; -moz-user-select: none;
|
||||
-ms-user-select: none; user-select: none;
|
||||
padding: 0 4px; width: 4em;
|
||||
color: #aaaaaa;
|
||||
}
|
||||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||||
div.sourceCode
|
||||
{ }
|
||||
@media screen {
|
||||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||||
}
|
||||
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
||||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
||||
code span.at { color: #7d9029; } /* Attribute */
|
||||
code span.bn { color: #40a070; } /* BaseN */
|
||||
code span.bu { } /* BuiltIn */
|
||||
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
||||
code span.ch { color: #4070a0; } /* Char */
|
||||
code span.cn { color: #880000; } /* Constant */
|
||||
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
||||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
||||
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
||||
code span.dt { color: #902000; } /* DataType */
|
||||
code span.dv { color: #40a070; } /* DecVal */
|
||||
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
||||
code span.ex { } /* Extension */
|
||||
code span.fl { color: #40a070; } /* Float */
|
||||
code span.fu { color: #06287e; } /* Function */
|
||||
code span.im { } /* Import */
|
||||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
||||
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
||||
code span.op { color: #666666; } /* Operator */
|
||||
code span.ot { color: #007020; } /* Other */
|
||||
code span.pp { color: #bc7a00; } /* Preprocessor */
|
||||
code span.sc { color: #4070a0; } /* SpecialChar */
|
||||
code span.ss { color: #bb6688; } /* SpecialString */
|
||||
code span.st { color: #4070a0; } /* String */
|
||||
code span.va { color: #19177c; } /* Variable */
|
||||
code span.vs { color: #4070a0; } /* VerbatimString */
|
||||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.3 Proofs and Algorithms I: Primality Testing</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>Now let’s see an example of applying the concept of mathematical proof to justify the correctness of an algorithm. First, recall that we say that an integer <span class="math inline">\(p\)</span> is <em>prime</em> when it is greater than 1 and the only numbers that divide <span class="math inline">\(p\)</span> are 1 and <span class="math inline">\(p\)</span> itself. We saw earlier that we could implement a predicate in Python to determine whether <code>p</code> is prime:</p>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">def</span> is_prime(p: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">bool</span>:</span>
|
||||
<span id="cb1-2"><a href="#cb1-2"></a> <span class="co">"""Return whether p is prime."""</span></span>
|
||||
<span id="cb1-3"><a href="#cb1-3"></a> possible_divisors <span class="op">=</span> <span class="bu">range</span>(<span class="dv">0</span>, p <span class="op">+</span> <span class="dv">1</span>)</span>
|
||||
<span id="cb1-4"><a href="#cb1-4"></a> <span class="cf">return</span> (</span>
|
||||
<span id="cb1-5"><a href="#cb1-5"></a> p <span class="op">></span> <span class="dv">1</span> <span class="kw">and</span></span>
|
||||
<span id="cb1-6"><a href="#cb1-6"></a> <span class="bu">all</span>({d <span class="op">==</span> <span class="dv">1</span> <span class="kw">or</span> d <span class="op">==</span> p <span class="cf">for</span> d <span class="kw">in</span> possible_divisors <span class="cf">if</span> divides(d, p)})</span>
|
||||
<span id="cb1-7"><a href="#cb1-7"></a> )</span></code></pre></div>
|
||||
<p>This implementation is a direct translation of the mathematical definition of prime numbers, with the only difference being our restriction of the range of possible divisors.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"> In fact, we can justify that this range is correct in a separate proof!</span> However, you might have noticed that this algorithm is “inefficient” because it checks more numbers than necessary.</p>
|
||||
<p>Often when this version of <code>is_prime</code> is taught, the range of possible divisors extends only to the square root of the input <code>p</code>:</p>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1"></a><span class="im">from</span> math <span class="im">import</span> floor, sqrt</span>
|
||||
<span id="cb2-2"><a href="#cb2-2"></a></span>
|
||||
<span id="cb2-3"><a href="#cb2-3"></a></span>
|
||||
<span id="cb2-4"><a href="#cb2-4"></a><span class="kw">def</span> is_prime(p: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">bool</span>:</span>
|
||||
<span id="cb2-5"><a href="#cb2-5"></a> <span class="co">"""Return whether p is prime."""</span></span>
|
||||
<span id="cb2-6"><a href="#cb2-6"></a> possible_divisors <span class="op">=</span> <span class="bu">range</span>(<span class="dv">2</span>, floor(sqrt(p)) <span class="op">+</span> <span class="dv">1</span>)</span>
|
||||
<span id="cb2-7"><a href="#cb2-7"></a> <span class="cf">return</span> (</span>
|
||||
<span id="cb2-8"><a href="#cb2-8"></a> p <span class="op">></span> <span class="dv">1</span> <span class="kw">and</span></span>
|
||||
<span id="cb2-9"><a href="#cb2-9"></a> <span class="bu">all</span>({<span class="kw">not</span> divides(d, p) <span class="cf">for</span> d <span class="kw">in</span> possible_divisors})</span>
|
||||
<span id="cb2-10"><a href="#cb2-10"></a> )</span></code></pre></div>
|
||||
<p>This version is intuitively faster, as the range of possible divisors to check is smaller. But how do we actually know that this version of <code>is_prime</code> is correct? We could write some tests, but as we discussed earlier both unit tests and property-based tests do not guarantee absolute correctness, they just give confidence. Luckily, for algorithms like this one that are based on the mathematical properties of the input, we do have a tool that guarantees absolutely certainty: proofs!</p>
|
||||
<h2 id="a-property-of-prime-numbers">A property of prime numbers</h2>
|
||||
<p>Formally, we can justify the correctness by formally proving the following statement.</p>
|
||||
<div class="theorem">
|
||||
<p>Let <span class="math inline">\(p \in \Z\)</span>. Then <span class="math inline">\(p\)</span> is prime if and only if <span class="math inline">\(p > 1\)</span> and for every integer <span class="math inline">\(d\)</span> in the range <span class="math inline">\(2 \leq d \leq \sqrt{p}\)</span>, <span class="math inline">\(d\)</span> does not divide <span class="math inline">\(p\)</span>.</p>
|
||||
<p>Or, translated into predicate logic: <span class="math display">\[\forall p \in \Z,~ \mathit{Prime}(p) \Leftrightarrow \big(p > 1 \land (\forall d \in \N,~ 2 \leq d \leq \sqrt{p} \Rightarrow d \nmid p) \big).\]</span></p>
|
||||
</div>
|
||||
<p>How do we go about proving that this statement is correct? We’ve seen in the past how to prove implications, but how about biconditionals? Recall that a biconditional <span class="math inline">\(p \Leftrightarrow q\)</span> is equivalent to <span class="math inline">\((p \Rightarrow q) \land (q \Rightarrow p)\)</span>. So if we want to argue that a biconditional is True, we do so by proving the two different implications.</p>
|
||||
<div class="framed">
|
||||
<p><strong>A typical proof of a biconditional.</strong></p>
|
||||
<p>Given statement to prove: <span class="math inline">\(p \Leftrightarrow q\)</span>.</p>
|
||||
<div class="proof">
|
||||
<p>This proof is divided into two parts.</p>
|
||||
<p><strong>Part 1</strong> (<span class="math inline">\(p \Rightarrow q\)</span>): Assume <span class="math inline">\(p\)</span>.</p>
|
||||
<p>[Proof that <span class="math inline">\(q\)</span> is True.]</p>
|
||||
<p><strong>Part 2</strong> (<span class="math inline">\(q \Rightarrow p\)</span>): Assume <span class="math inline">\(q\)</span>.</p>
|
||||
<p>[Proof that <span class="math inline">\(p\)</span> is True.]</p>
|
||||
</div>
|
||||
</div>
|
||||
<h3 id="proving-the-first-implication">Proving the first implication</h3>
|
||||
<div class="discussion">
|
||||
<p>The first implication we’ll prove is that if <span class="math inline">\(p\)</span> is prime, then <span class="math inline">\(p > 1\)</span> and <span class="math inline">\(\forall d \in \N,~ 2 \leq d \leq \sqrt{p} \Rightarrow d \nmid p\)</span>. We get to assume that <span class="math inline">\(p\)</span> is prime, and will need to prove two things: that <span class="math inline">\(p > 1\)</span>, and that <span class="math inline">\(\forall d \in \N,~ 2 \leq d \leq \sqrt{p} \Rightarrow d \nmid p\)</span>.</p>
|
||||
<p>Let’s remind ourselves what the definition of prime is in predicate logic:</p>
|
||||
<p><span class="math display">\[\mathit{Prime}(p):~ p > 1 \land \big(\forall d \in \N,~ d \mid p \Rightarrow d = 1 \lor d = p \big)\]</span></p>
|
||||
<p>The first part comes straight from the definition of prime. For the second part, we should also be able to use the definition of prime: if <span class="math inline">\(d\)</span> is between 2 and <span class="math inline">\(\sqrt{p}\)</span>, then it can’t equal 1 or <span class="math inline">\(p\)</span>, which are the only possible divisors of <span class="math inline">\(p\)</span>.</p>
|
||||
<p>Let’s see how to write this up formally.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(p \in \Z\)</span> and assume that <span class="math inline">\(p\)</span> is prime. We need to prove that <span class="math inline">\(p > 1\)</span> and for all <span class="math inline">\(d \in \N\)</span>, if <span class="math inline">\(2 \leq d \leq \sqrt p\)</span> then <span class="math inline">\(d\)</span> does not divide <span class="math inline">\(p\)</span>.</p>
|
||||
<p><strong>Part 1</strong>: proving that <span class="math inline">\(p > 1\)</span>.</p>
|
||||
<p>By the definition of prime, we know that <span class="math inline">\(p > 1\)</span>.</p>
|
||||
<p><strong>Part 2</strong>: proving that for all <span class="math inline">\(d \in \N\)</span>, if <span class="math inline">\(2 \leq d \leq \sqrt p\)</span> then <span class="math inline">\(d\)</span> does not divide <span class="math inline">\(p\)</span>.</p>
|
||||
<p>Let <span class="math inline">\(d \in \N\)</span> and assume <span class="math inline">\(2 \leq d \leq \sqrt p\)</span>. We’ll prove that <span class="math inline">\(d\)</span> does not divide <span class="math inline">\(p\)</span>.</p>
|
||||
<p>First, since <span class="math inline">\(2 \leq d\)</span>, we know <span class="math inline">\(d > 1\)</span>, and so <span class="math inline">\(d \neq 1\)</span>. Second, since <span class="math inline">\(p > 1\)</span>, we know that <span class="math inline">\(\sqrt p < p\)</span>, and so <span class="math inline">\(d \leq \sqrt p < p\)</span>.</p>
|
||||
<p>This means that <span class="math inline">\(d \neq 1\)</span> and <span class="math inline">\(d \neq p\)</span>. By the definition of prime again, we can conclude that <span class="math inline">\(d \nmid p\)</span>.</p>
|
||||
</div>
|
||||
<p>What we’ve proved so far is that if <span class="math inline">\(p\)</span> is prime, then it has no divisors between 2 and <span class="math inline">\(\sqrt p\)</span>. How does this apply to our algorithm <code>is_prime</code>? When its input <code>p</code> is a prime number, we know that the expressions <code>p > 1</code> and <code>all(not divides(d, p) for d in possible_divisors)</code> will both evaluate to <code>True</code>, and so the function will return <code>True</code>. In other words, we’ve proven that <code>is_prime</code> returns the correct value for <em>every</em> prime number, without a single test case! Pretty awesome.</p>
|
||||
<h3 id="proving-the-second-implication">Proving the second implication</h3>
|
||||
<p>Though we know that <code>is_prime</code> is correct for prime numbers, we’ve said nothing at all about how it behaves when given a non-prime number. To prove that its behaviour is correct in this case as well, we need to prove the other conditional.</p>
|
||||
<div class="discussion">
|
||||
<p>We now need to prove the second implication, which is the converse of the first: if <span class="math inline">\(p > 1\)</span> and <span class="math inline">\(\forall d \in \N,~ 2 \leq d \leq \sqrt{p} \Rightarrow d \nmid p\)</span>, then <span class="math inline">\(p\)</span> must be prime. Expanding the definition of prime, we need to prove that <span class="math inline">\(p > 1\)</span> (which we’ve assumed!) and that for all <span class="math inline">\(d_1 \in \N,~ d_1 \mid p \Rightarrow d_1 = 1 \lor d_1 = p\)</span>.</p>
|
||||
<p>So the idea here is to let <span class="math inline">\(d_1 \in \N\)</span> and assume <span class="math inline">\(d_1 \mid p\)</span>, and use the condition that <span class="math inline">\(\forall d \in \N,~ 2 \leq d \leq \sqrt{p} \Rightarrow d \nmid p\)</span> to prove that <span class="math inline">\(d_1\)</span> is 1 or <span class="math inline">\(p\)</span>.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(p \in \N\)</span>, and assume <span class="math inline">\(p > 1\)</span> and that <span class="math inline">\(\forall d \in \N,~ 2 \leq d \leq \sqrt{p} \Rightarrow d \nmid p\)</span>. We want to prove that <span class="math inline">\(p\)</span> is prime, i.e., that <span class="math inline">\(p > 1\)</span> and that <span class="math inline">\(\forall d_1 \in \N,~ d_1 \mid p \Rightarrow d_1 = 1 \lor d_1 = p\)</span>.</p>
|
||||
<p>We know the first part (<span class="math inline">\(p > 1\)</span>) is true because it’s one of our assumptions. For the second part, first let <span class="math inline">\(d_1 \in \N\)</span>, and assume <span class="math inline">\(d_1 \mid p\)</span>. We’ll prove that <span class="math inline">\(d_1 = 1 \lor d_1 = p\)</span>.</p>
|
||||
<p>From our second assumption, we know that since <span class="math inline">\(d_1 \mid p\)</span>, it is not between 2 and <span class="math inline">\(\sqrt p\)</span>.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> More precisely, the <em>contrapositive</em> of our second assumption says that for all <span class="math inline">\(d \in \N\)</span>, <span class="math inline">\(d \mid p \Rightarrow d < 2 \lor d > \sqrt p\)</span>.</span> So then either <span class="math inline">\(d_1 < 2\)</span> or <span class="math inline">\(d_1 > \sqrt p\)</span>. We divide our proof into two <strong>cases</strong> based on these possibilities.</p>
|
||||
<p><strong>Case 1</strong>: assume <span class="math inline">\(d_1 < 2\)</span>.</p>
|
||||
<p>Since <span class="math inline">\(d_1 \in \N\)</span>, it must be 0 or 1 in this case. We know <span class="math inline">\(0 \nmid p\)</span> because <span class="math inline">\(p > 1\)</span>, and so <span class="math inline">\(d_1 = 1\)</span>.</p>
|
||||
<p><strong>Case 2</strong>: assume <span class="math inline">\(d_1 > \sqrt p\)</span>.</p>
|
||||
<p>Since we assumed <span class="math inline">\(d_1 \mid p\)</span>, we expand the definition of divisibility to conclude that <span class="math inline">\(\exists k \in \Z,~ p = d_1 k\)</span>. Since <span class="math inline">\(d_1 > \sqrt p\)</span> in this case, we know that <span class="math inline">\(k = \frac{p}{d_1} < \frac{p}{\sqrt{p}} = \sqrt{p}\)</span>.</p>
|
||||
<p>Since <span class="math inline">\(p = d_1k\)</span>, we know that <span class="math inline">\(k \mid p\)</span> as well, and so our second assumption applied to <span class="math inline">\(k\)</span> tells us that <span class="math inline">\(k\)</span> is not between 2 and <span class="math inline">\(\sqrt p\)</span>.</p>
|
||||
<p>So <span class="math inline">\(k < \sqrt{p}\)</span> and is not between 2 and <span class="math inline">\(\sqrt p\)</span>. Therefore <span class="math inline">\(k = 1\)</span>, and so <span class="math inline">\(d_1 = \frac{p}{k} = p\)</span>.</p>
|
||||
</div>
|
||||
<p>To wrap up this example, let’s see how this implication connects to our function <code>is_prime</code>. What we’ve proved is that if <code>is_prime(p)</code> returns <code>True</code>, then <code>p</code> must be prime. This sounds very similar to what we said in the previous section, but it is different! The contrapositive this statement here is useful: if <code>p</code> is NOT prime, then <code>is_prime(p)</code> returns <code>False</code>.</p>
|
||||
<p>So putting the two implications together, we have:</p>
|
||||
<ul>
|
||||
<li>For all integers <code>p</code>, if <code>p</code> is prime then <code>is_prime(p)</code> returns <code>True</code>.</li>
|
||||
<li>For all integers <code>p</code>, if <code>is_prime(p)</code> returns <code>True</code> then <code>p</code> is prime.<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote"> Or equivalently, if <code>p</code> is not prime then <code>is_prime(p)</code> returns <code>False</code>.</span></li>
|
||||
</ul>
|
||||
<p>Since every integer <code>p</code> is either prime or not prime, we can conclude that this implementation of <code>is_prime</code> is <strong>correct</strong> according to its specification.</p>
|
||||
<h2 id="algorithm-correctness-and-theoretical-properties">Algorithm correctness and theoretical properties</h2>
|
||||
<p>Notice the duality between the statement of correctness for <code>is_prime</code> and the biconditional we had set out to prove: for every natural number <span class="math inline">\(p\)</span>, <span class="math inline">\(p\)</span> is prime if and only if <span class="math inline">\(p > 1\)</span> and for every integer <span class="math inline">\(d\)</span> in the range <span class="math inline">\(2 \leq d \leq \sqrt{p}\)</span>, <span class="math inline">\(d \nmid p\)</span>. The correctness of our <em>algorithm</em> is derived from the <em>theoretical properties of prime numbers</em> that we expressed in formal predicate logic. We admit this is a relatively simple example of this connection between algorithm and mathematical theory, but we had to start somewhere! Our future examples will draw on connections like this, but in far deeper ways.</p>
|
||||
</section>
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,166 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.4 Proof by Cases and Disproofs</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.4 Proof by Cases and Disproofs</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>In the last proof of the previous section, we did something interesting: having concluded that <span class="math inline">\(d_1 < 2\)</span> or <span class="math inline">\(d_1 > \sqrt p\)</span>, we proceeded to split up our proof into two cases, one where we assumed that each part of the OR was true. This is a proof technique known as <strong>proof by cases</strong>.</p>
|
||||
<h2 id="proof-by-cases">Proof by cases</h2>
|
||||
<p>Remember that for a universal proof, we typically let a variable be an arbitrary element of the domain, and then make an argument in the proof body to prove our goal statement. However, even when the goal statement is True for all elements of the domain, it isn’t always easy to construct a single argument that works for all of those elements! Sometimes, different arguments are required for different elements. In this case, we divide the domain into different parts, and then write a separate argument for each part.</p>
|
||||
<p>A bit more formally, we pick a set of unary predicates <span class="math inline">\(P_1\)</span>, <span class="math inline">\(P_2\)</span>, …, <span class="math inline">\(P_k\)</span> (for some positive integer <span class="math inline">\(k\)</span>), such that for every element <span class="math inline">\(x\)</span> in the domain, <span class="math inline">\(x\)</span> satisfies at least one of the predicates (we say that these predicates are <em>exhaustive</em>). Note that the domain can be narrowed based on additional assumptions or conclusions made earlier in the proof. In our previous example, we started with a domain “<span class="math inline">\(d_1 \in \N\)</span>”, and then narrowed this to “<span class="math inline">\(d_1 \in \N\)</span> and <span class="math inline">\((d_1 < 2 \lor d_1 > \sqrt p)\)</span>”, leading to the following predicates for our cases: <span class="math display">\[P_1(d_1): d_1 < 2, \qquad P_2(d_1): d_1 > \sqrt p.\]</span></p>
|
||||
<p>Then, we divide the proof body into cases, where in each case we <em>assume</em> that one of the predicates is True, and use that assumption to construct a proof that specifically works under that assumption.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"> Recall that there’s an equivalence between predicates and sets. Another way of looking at a proof by cases is that we divide the domain into subsets <span class="math inline">\(S_1, S_2, \dots S_k\)</span>, and then prove the desired statement separately for each of these subsets.</span></p>
|
||||
<div class="framed">
|
||||
<p><strong>A typical proof by cases.</strong></p>
|
||||
<p>Given statement to prove: <span class="math inline">\(\forall x \in S, P(x).\)</span> Pick a set of exhaustive predicates <span class="math inline">\(P_1, \dots, P_k\)</span> of <span class="math inline">\(S\)</span>.</p>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(x \in S\)</span>. We will use a proof by cases.</p>
|
||||
<p><strong>Case 1</strong>. <em>Assume</em> <span class="math inline">\(P_1(x)\)</span> is True.</p>
|
||||
<p>[Proof that <span class="math inline">\(P(x)\)</span> is True, assuming <span class="math inline">\(P_1(x)\)</span>.]</p>
|
||||
<p><strong>Case 2</strong>. <em>Assume</em> <span class="math inline">\(P_2(x)\)</span> is True.</p>
|
||||
<p>[Proof that <span class="math inline">\(P(x)\)</span> is True, assuming <span class="math inline">\(P_2(x)\)</span>.]</p>
|
||||
<p><span class="math inline">\(\vdots\)</span></p>
|
||||
<p><strong>Case <span class="math inline">\(k\)</span></strong>. <em>Assume</em> <span class="math inline">\(P_k(x)\)</span> is True.</p>
|
||||
<p>[Proof that <span class="math inline">\(P(x)\)</span> is True, assuming <span class="math inline">\(P_k(x)\)</span>.]</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Proof by cases is a very versatile proof technique, since it allows the combining of simpler proofs together to form a whole proof. Often it is easier to prove a property about some (or even most) elements of the domain than it is to prove that same property about all the elements. But do keep in mind that if you can find a <em>simple</em> proof which works for all elements of the domain, that’s generally preferable than combining multiple proofs together in a proof by cases.</p>
|
||||
<h2 id="cases-and-the-quotient-remainder-theorem">Cases and the Quotient-Remainder Theorem</h2>
|
||||
<p>One natural use of proof by cases in number theory is to apply the Quotient-Remainder Theorem that we introduced in <a href="01-number-theory-definitions.html">Section 6.1</a>.</p>
|
||||
<div id="Quotient-Remainder Theorem" class="theorem">
|
||||
<p>(Quotient-Remainder Theorem) For all <span class="math inline">\(n \in \Z\)</span> and <span class="math inline">\(d \in \Z^+\)</span>, there exist <span class="math inline">\(q \in \Z\)</span> and <span class="math inline">\(r \in \N\)</span> such that <span class="math inline">\(n = qd + r\)</span> and <span class="math inline">\(0 \leq r < d\)</span>. Moreover, these <span class="math inline">\(q\)</span> and <span class="math inline">\(r\)</span> are <em>unique</em> for a given <span class="math inline">\(n\)</span> and <span class="math inline">\(d\)</span>.</p>
|
||||
<p>We say that <span class="math inline">\(q\)</span> is the <strong>quotient</strong> when <span class="math inline">\(n\)</span> is divided by <span class="math inline">\(d\)</span>, and that <span class="math inline">\(r\)</span> is the <strong>remainder</strong> when <span class="math inline">\(n\)</span> is divided by <span class="math inline">\(d\)</span>.</p>
|
||||
</div>
|
||||
<p>The reason this theorem is powerful is that it tells us that for any non-zero divisor <span class="math inline">\(d \in \Z^+\)</span>, we can separate all possible integers into <span class="math inline">\(d\)</span> different groups, corresponding to their possible remainders (between <span class="math inline">\(0\)</span> and <span class="math inline">\(d-1\)</span>) when divided by <span class="math inline">\(d\)</span>. Let’s see this how to use this fact to perform a proof by cases.</p>
|
||||
<div class="example">
|
||||
<p>Prove that for all integers <span class="math inline">\(x\)</span>, <span class="math inline">\(2 \DIV x^2 + 3x\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>Using the divisibility predicate: <span class="math inline">\(\forall x \in \Z,~ 2 \DIV x^2 + 3x\)</span>. Or expanding the definition of divisibility: <span class="math display">\[\forall x \in \Z,~ \exists k \in \Z,~ x^2 + 3x = 2k.\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>We want to “factor out a <span class="math inline">\(2\)</span>” from the expression <span class="math inline">\(x^2 + 3x\)</span>, but this only works if <span class="math inline">\(x\)</span> is even. If <span class="math inline">\(x\)</span> is odd, though, then both <span class="math inline">\(x^2\)</span> and <span class="math inline">\(3x\)</span> will be odd, and adding two odd numbers together produces an even number.</p>
|
||||
<p>But how do we “know” that every number has to be either even or odd? And how can we formalize the algebraic operations of “factoring out a <span class="math inline">\(2\)</span>” or “adding two odd numbers together”? This is where the Quotient-Remainder Theorem comes in.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(x \in \Z\)</span>. By the Quotient-Remainder Theorem, we know that when <span class="math inline">\(x\)</span> is divided by <span class="math inline">\(2\)</span>, the two possible remainders are <span class="math inline">\(0\)</span> and <span class="math inline">\(1\)</span>. We will divide up the proof into two cases based on these remainders.</p>
|
||||
<p><strong>Case 1</strong>: assume the remainder when <span class="math inline">\(x\)</span> is divided by <span class="math inline">\(2\)</span> is <span class="math inline">\(0\)</span>. That is, we assume there exists <span class="math inline">\(q \in \Z\)</span> such that <span class="math inline">\(x = 2q + 0\)</span>. We will show that there exists <span class="math inline">\(k \in \Z\)</span> such that <span class="math inline">\(x^2 + 3x = 2k\)</span>.</p>
|
||||
<p>We have: <span class="math display">\[\begin{align*}
|
||||
x^2 + 3x &= (2q)^2 + 3(2q) \\
|
||||
&= 4q^2 + 6q \\
|
||||
&= 2(2q^2 + 3q)
|
||||
\end{align*}\]</span></p>
|
||||
<p>So let <span class="math inline">\(k = 2q^2 + 3q\)</span>. Then <span class="math inline">\(x^2 + 3x = 2k\)</span>.</p>
|
||||
<p><strong>Case 2</strong>: assume the remainder when <span class="math inline">\(x\)</span> is divided by 2 is <span class="math inline">\(1\)</span>. That is, we assume there exists <span class="math inline">\(q \in \Z\)</span> such that <span class="math inline">\(x = 2q + 1\)</span>. We will show that there exists <span class="math inline">\(k \in \Z\)</span> such that <span class="math inline">\(x^2 + 3x = 2k\)</span>.</p>
|
||||
<p>We have: <span class="math display">\[\begin{align*}
|
||||
x^2 + 3x &= (2q+1)^2 + 3(2q+1) \\
|
||||
&= 4q^2 + 4q + 1 + 6q + 3 \\
|
||||
&= 2(2q^2 + 5q + 2)
|
||||
\end{align*}\]</span></p>
|
||||
<p>So let <span class="math inline">\(k = 2q^2 + 5q + 2\)</span>. Then <span class="math inline">\(x^2 + 3x = 2k\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="false-statements-and-disproofs">False statements and disproofs</h2>
|
||||
<p>Suppose we have a friend who is trying to convince us that a certain statement <span class="math inline">\(X\)</span> is False. If they tell you that statement <span class="math inline">\(X\)</span> is false because they tried really hard to come up with a proof of it and failed, you might believe them, or you might wonder if maybe they just missed a crucial idea leading to a correct proof.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">Maybe they skipped all their CSC110 classes.</span> An absence of proof is not enough to convince us that the statement is False.</p>
|
||||
<p>Instead, we must see a <strong>disproof</strong>, which is simply a proof that the <em>negation</em> of the statement is True.<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote">In other words, if we can prove that <span class="math inline">\(\NOT X\)</span> is True, then <span class="math inline">\(X\)</span> must be False.</span> For this section, we’ll be using the simplification rules from <a href="../03-logic/02-predicate-logic.html">Section 3.2</a> to make negations of statements easier to work with.</p>
|
||||
<div class="example">
|
||||
<p>Disprove the following statement: every natural number divides 360.</p>
|
||||
<div class="translation">
|
||||
<p>This statement can be written as <span class="math inline">\(\forall n \in \N,~n \DIV 360\)</span>. However, we want to prove that it is False, so we really need to study its negation. <span class="math display">\[\begin{align*}
|
||||
\NOT \big(\forall n \in \N,~n \DIV 360 \big) \\
|
||||
\exists n \in \N,~ n \NDIV 360
|
||||
\end{align*}\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>The original statement is obviously not True: the number 7 doesn’t divide 360, for instance. Is that a proof? We wrote the negation of the statement in symbolic form above, and if we translate it back into English, we get “there exists a natural number which does not divide 360.” So, yes. That’s enough for a proof.</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(n = 7\)</span>.</p>
|
||||
<p>Then <span class="math inline">\(n \NDIV 360\)</span>, since <span class="math inline">\(\frac{360}{7} = 51.428\dots\)</span> is not an integer.</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>When we want disprove a universally-quantified statement (“every element of <span class="math inline">\(S\)</span> satisfies predicate <span class="math inline">\(P\)</span>”), the negation of that statement becomes an existentially-quantified one (“there exists an element of <span class="math inline">\(S\)</span> that doesn’t satisfy predicate <span class="math inline">\(P\)</span>”). Since proofs of existential quantification involve just finding one value, the disproof of the original statement involves finding such a value which causes the predicate to be False (or alternatively, causes the negation of the predicate to be True). We call this value a <strong>counterexample</strong> for the original statement. In the previous example, we would say that 7 is a counterexample of the given statement.</p>
|
||||
<div class="framed">
|
||||
<p><strong>A typical disproof of a universal (counterexample).</strong></p>
|
||||
<p>Given statement to <em>disprove</em>: <span class="math inline">\(\forall x \in S,~P(x)\)</span>.</p>
|
||||
<div class="proof">
|
||||
<p>We prove the negation, <span class="math inline">\(\exists x \in S,~\NOT P(x)\)</span>. Let <span class="math inline">\(x=\)</span> <code>_______</code>.</p>
|
||||
<p>[Proof that <span class="math inline">\(\NOT P\)</span>(<code>_______</code>) is True.]</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,147 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.5 Greatest Common Divisor</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.5 Greatest Common Divisor</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>In this section, we’ll take a closer look at the <em>greatest common divisor</em> of two numbers. Recall the following definitions from <a href="01-number-theory-definitions.html">6.1 An Introduction to Number Theory</a>.</p>
|
||||
<div class="definition" data-terms="common divisor, greatest common divisor">
|
||||
<p>Let <span class="math inline">\(x, y, d \in \Z\)</span>. We say that <span class="math inline">\(d\)</span> is a <strong>common divisor</strong> of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> when <span class="math inline">\(d\)</span> divides <span class="math inline">\(x\)</span> and <span class="math inline">\(d\)</span> divides <span class="math inline">\(y\)</span>.</p>
|
||||
<p>We say that <span class="math inline">\(d\)</span> is the <strong>greatest common divisor</strong> of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> when it is the largest number that is a common divisor of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>, or 0 when <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> are both 0. We can define the function <span class="math inline">\(\gcd : \Z \times \Z \to \N\)</span> as the function which takes numbers <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>, and returns their greatest common divisor.</p>
|
||||
</div>
|
||||
<p>To make it easier to translate this statement into symbolic form, we can restate the “maximum” part by saying that if <span class="math inline">\(e\)</span> is any number which divides <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span>, then <span class="math inline">\(e \leq d\)</span>. Let <span class="math inline">\(m, n, d \in \Z\)</span>, and suppose <span class="math inline">\(d = \gcd(m, n)\)</span>. Then <span class="math inline">\(d\)</span> satisfies the following statement:</p>
|
||||
<p><span class="math display">\[\begin{align*}
|
||||
&\Big(m = 0 \land n = 0 \Rightarrow d = 0 \Big)~\land \\
|
||||
&\Big(m \neq 0 \lor n \neq 0 \Rightarrow \\
|
||||
& \qquad d \mid m \land d \mid n \land \big(\forall e \in \N,~e \mid m \land e \mid n \Rightarrow e \leq d\big) \Big)
|
||||
\end{align*}\]</span></p>
|
||||
<p>This expression has a few subtleties. First, because we actually have separate definitions for <span class="math inline">\(\gcd(m, n)\)</span> when both arguments are zero and when at least one of them is non-zero, these two definitions are expressed as two different implications.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"> This is analogous to writing an if statement in Python. In this case, we’re saying that only one of the conclusions needs to be True, depending on which of the hypotheses are True.</span></p>
|
||||
<p>Here is an example proof which makes use of both this definition, and the definition of prime.</p>
|
||||
<div class="example">
|
||||
<p>Prove that for all integers <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span>, if <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span> are distinct primes, then <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span> are <em>coprime</em>, meaning <span class="math inline">\(\gcd(p, q) = 1\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>Here is an initial translation which focuses on the structure of the above statement, but doesn’t unpack any definitions: <span class="math display">\[\forall p, q \in \Z,~\big(Prime(p) \land Prime(q) \land p \neq q\big) \IMP \gcd(p, q) = 1.\]</span> We could unpack the definitions of <span class="math inline">\(Prime\)</span> and gcd, but doing so would not add any insight at this point. While we will almost certainly end up using these definitions in the discussion and proof sections, expanding it here actually obscures the meaning of the statement.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> In general, use translation as a way of precisely specifying the <em>structure</em> of a statement; as we have seen repeatedly, the high-level structure of a statement is mimicked in the structure of its proof. And while you don’t need to expand every definition in a statement, you should <em>always</em> keep in mind that definitions referred to in the statement will require unpacking in the proof itself.</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>We know that primes don’t have many divisors, and that <span class="math inline">\(1\)</span> is a common divisor for any pair of numbers. So to show that <span class="math inline">\(\gcd(p, q) = 1\)</span>, we just need to make sure that neither <span class="math inline">\(p\)</span> nor <span class="math inline">\(q\)</span> divides the other (otherwise that would be a common divisor larger than <span class="math inline">\(1\)</span>).</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(p, q \in \Z\)</span>. Assume that <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span> are both prime, and that <span class="math inline">\(p \neq q\)</span>. We want to prove that <span class="math inline">\(\gcd(p, q) = 1\)</span>.</p>
|
||||
<p>By the definition of prime, we know that <span class="math inline">\(p \neq 1\)</span> (since <span class="math inline">\(p > 1\)</span>). Also by the definition of prime, the only positive divisors of <span class="math inline">\(q\)</span> are <span class="math inline">\(1\)</span> and <span class="math inline">\(q\)</span> itself. So then since <span class="math inline">\(p \neq q\)</span> (our assumption) and <span class="math inline">\(p \neq 1\)</span>, we know that <span class="math inline">\(p \NDIV q\)</span>.</p>
|
||||
<p>Next, we know that <span class="math inline">\(1\)</span> divides every number<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote">We proved this in Section 6.2!</span>, and so <span class="math inline">\(1\)</span> is the only positive common divisor of <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span>, so <span class="math inline">\(\gcd(p, q) = 1\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>In the above proof, we did something new in the last paragraph: we referred to a statement we had proved to justify a step in the proof. This might sound kind of funny—after all, many of our proofs so far have relied on some algebraic manipulations which are valid but are really knowledge we learned prior to this course. The subtle difference is that those algebraic laws we take for granted as “obvious” because we learned them so long ago. But in fact our proofs can consist of steps which are statements that we know are true because of an external source, even one that <em>we don’t know how to prove ourselves</em>.</p>
|
||||
<p>This is a fundamental parallel between writing proofs and writing computer programs. In programming, we start with some basic building blocks of a language—data types, control flow constructs, etc.—but we often rely on libraries as well to simplify our tasks. We can use these libraries by reading their documentation and understanding how to use them, but don’t need to understand how they are implemented. In the same way, we can use an external theorem in our proof by understanding what it means, but without knowing how to prove it.</p>
|
||||
<p>Let’s look at one example of this in action.</p>
|
||||
<h2 id="linear-combinations-and-the-greatest-common-divisor">Linear combinations and the greatest common divisor</h2>
|
||||
<p>First, a “helper” definition:</p>
|
||||
<div class="definition" data-terms="linear-combination">
|
||||
<p>Let <span class="math inline">\(m, n, a \in \Z\)</span>. We say that <span class="math inline">\(a\)</span> is a <strong>linear combination of <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span></strong> when there exist <span class="math inline">\(p, q \in \Z\)</span> such that <span class="math inline">\(a = pm + qn\)</span>.</p>
|
||||
</div>
|
||||
<p>For example, 101 is a linear combination of 5 and 3, since <span class="math inline">\(101 = 10 \cdot 5 + 17 \cdot 3\)</span>.</p>
|
||||
<p>We can use this definition to state one fairly straightforward property of divisibility, and one surprising property of the greatest common divisor.</p>
|
||||
<div id="theorem:divide_lin_comb" class="theorem">
|
||||
<p>(<em>Divisibility of Linear Combinations</em>) Let <span class="math inline">\(m, n, d \in \Z\)</span>. If <span class="math inline">\(d\)</span> divides <span class="math inline">\(m\)</span> and <span class="math inline">\(d\)</span> divides <span class="math inline">\(n\)</span>, then <span class="math inline">\(d\)</span> divides every linear combination of <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span>.</p>
|
||||
</div>
|
||||
<div id="theorem:gcd_characterization" class="theorem">
|
||||
<p>(<em>GCD Characterization</em>) Let <span class="math inline">\(m, n \in \Z\)</span>, and assume at least one of them is non-zero. Then <span class="math inline">\(\gcd(m, n)\)</span> is the smallest positive integer that is a linear combination of <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span>.</p>
|
||||
</div>
|
||||
<p>Next, we’ll see how to use these two theorems as “helpers” inside a proof of the following statement, which is yet another property of the greatest common divisor.</p>
|
||||
<div class="example">
|
||||
<p>For all <span class="math inline">\(m, n, d \in \Z\)</span>, if <span class="math inline">\(d\)</span> divides both <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span> then <span class="math inline">\(d\)</span> also divides <span class="math inline">\(\gcd(m, n)\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>We can translate this statement as follows: <span class="math display">\[\forall m, n, d \in \Z,~ d \mid m \land d \mid n \Rightarrow d \mid \gcd(m, n).\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>This one is a bit tougher. All we know from the definition of gcd is that <span class="math inline">\(d \leq \gcd(m, n)\)</span>, but that doesn’t imply <span class="math inline">\(d \mid \gcd(m, n)\)</span> by any means.</p>
|
||||
<p>But given the context that we just discussed in the preceding paragraphs, I’d guess that we should also use the <a href="#theorem:gcd_characterization">GCD Characterization Theorem</a> to write <span class="math inline">\(\gcd(m, n)\)</span> as <span class="math inline">\(pm + qn\)</span>. Oh, and the theorem before that one said that any number that divides <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span> will divide <span class="math inline">\(pm + qn\)</span> as well!</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(m, n, d \in \Z\)</span>. Assume that <span class="math inline">\(d \mid m\)</span> and <span class="math inline">\(d \mid n\)</span>. We want to prove that <span class="math inline">\(d \mid \gcd(m, n)\)</span>. We’ll divide our proof into two cases.<label for="sn-3" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-3" class="margin-toggle"/><span class="sidenote"> After reading the next two cases, answer: why did we need to divide our proof into cases? Is there another way we could have written this proof?</span></p>
|
||||
<p><strong>Case 1</strong>: assume <span class="math inline">\(m = 0\)</span> and <span class="math inline">\(n = 0\)</span>.</p>
|
||||
<p>In this case, by the definition of <span class="math inline">\(\gcd\)</span> we know that <span class="math inline">\(\gcd(m, n) = 0\)</span>. So <span class="math inline">\(d \mid \gcd(m, n)\)</span>, since we assumed that <span class="math inline">\(d\)</span> divides <span class="math inline">\(m\)</span> and <span class="math inline">\(n\)</span>, which are 0.</p>
|
||||
<p><strong>Case 2</strong>: assume <span class="math inline">\(m \neq 0\)</span> or <span class="math inline">\(n \neq 0\)</span>.</p>
|
||||
<p>Then By the <a href="#theorem:gcd_characterization">GCD Characterization Theorem</a>, there exist integers <span class="math inline">\(p, q \in \Z\)</span> such that <span class="math inline">\(\gcd(m, n) = pm + qn\)</span>.<label for="sn-4" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-4" class="margin-toggle"/><span class="sidenote">This line uses a known external fact that is an existential to <em>introduce</em> two variables <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span> to use in our proof.</span></p>
|
||||
<p>Then by the the <a href="#theorem:divide_lin_comb">Divisibility of Linear Combinations Theorem</a>, since <span class="math inline">\(d \mid m\)</span> and <span class="math inline">\(d \mid n\)</span> (by assumption), we know that <span class="math inline">\(d \mid pm + qn\)</span>.</p>
|
||||
<p>Therefore <span class="math inline">\(d \mid \gcd(m, n)\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,430 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.6 Proofs and Algorithms II: Computing the Greatest Common Divisor</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||||
div.sourceCode { margin: 1em 0; }
|
||||
pre.sourceCode { margin: 0; }
|
||||
@media screen {
|
||||
div.sourceCode { overflow: auto; }
|
||||
}
|
||||
@media print {
|
||||
pre > code.sourceCode { white-space: pre-wrap; }
|
||||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||||
}
|
||||
pre.numberSource code
|
||||
{ counter-reset: source-line 0; }
|
||||
pre.numberSource code > span
|
||||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||||
pre.numberSource code > span > a:first-child::before
|
||||
{ content: counter(source-line);
|
||||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||||
border: none; display: inline-block;
|
||||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||||
-khtml-user-select: none; -moz-user-select: none;
|
||||
-ms-user-select: none; user-select: none;
|
||||
padding: 0 4px; width: 4em;
|
||||
color: #aaaaaa;
|
||||
}
|
||||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||||
div.sourceCode
|
||||
{ }
|
||||
@media screen {
|
||||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||||
}
|
||||
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
||||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
||||
code span.at { color: #7d9029; } /* Attribute */
|
||||
code span.bn { color: #40a070; } /* BaseN */
|
||||
code span.bu { } /* BuiltIn */
|
||||
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
||||
code span.ch { color: #4070a0; } /* Char */
|
||||
code span.cn { color: #880000; } /* Constant */
|
||||
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
||||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
||||
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
||||
code span.dt { color: #902000; } /* DataType */
|
||||
code span.dv { color: #40a070; } /* DecVal */
|
||||
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
||||
code span.ex { } /* Extension */
|
||||
code span.fl { color: #40a070; } /* Float */
|
||||
code span.fu { color: #06287e; } /* Function */
|
||||
code span.im { } /* Import */
|
||||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
||||
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
||||
code span.op { color: #666666; } /* Operator */
|
||||
code span.ot { color: #007020; } /* Other */
|
||||
code span.pp { color: #bc7a00; } /* Preprocessor */
|
||||
code span.sc { color: #4070a0; } /* SpecialChar */
|
||||
code span.ss { color: #bb6688; } /* SpecialString */
|
||||
code span.st { color: #4070a0; } /* String */
|
||||
code span.va { color: #19177c; } /* Variable */
|
||||
code span.vs { color: #4070a0; } /* VerbatimString */
|
||||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.6 Proofs and Algorithms II: Computing the Greatest Common Divisor</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>In the previous section, we studied some mathematical properties of the greatest common divisor. Now in this section, we’ll look at how to implement algorithms for calculating the greatest common divisor, and introduce a new form of Python loops along the way.</p>
|
||||
<h2 id="naively-searching-for-the-gcd">Naively searching for the gcd</h2>
|
||||
<p>In this chapter we have used the divides predicate (e.g., <span class="math inline">\(d \mid n\)</span>) liberally. In <a href="../03-logic/09-working-with-definitions.html">Section 3.9</a>, we saw a possible implementation of the predicate as a function called <code>divides</code>:</p>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">def</span> divides(d: <span class="bu">int</span>, n: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">bool</span>:</span>
|
||||
<span id="cb1-2"><a href="#cb1-2"></a> <span class="co">"""Return whether d divides n."""</span></span>
|
||||
<span id="cb1-3"><a href="#cb1-3"></a> <span class="cf">if</span> d <span class="op">==</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb1-4"><a href="#cb1-4"></a> <span class="cf">return</span> n <span class="op">==</span> <span class="dv">0</span></span>
|
||||
<span id="cb1-5"><a href="#cb1-5"></a> <span class="cf">else</span>:</span>
|
||||
<span id="cb1-6"><a href="#cb1-6"></a> <span class="cf">return</span> n <span class="op">%</span> d <span class="op">==</span> <span class="dv">0</span></span></code></pre></div>
|
||||
<p>With this function in hand, we can implement a <code>gcd</code> function as follows:<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"> In this implementation, we use <code>abs</code> because <code>m</code> and/or <code>n</code> might be negative.</span></p>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1"></a><span class="kw">def</span> naive_gcd(m: <span class="bu">int</span>, n: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">int</span>:</span>
|
||||
<span id="cb2-2"><a href="#cb2-2"></a> <span class="co">"""Return the gcd of m and n."""</span></span>
|
||||
<span id="cb2-3"><a href="#cb2-3"></a> <span class="cf">if</span> m <span class="op">==</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb2-4"><a href="#cb2-4"></a> <span class="cf">return</span> <span class="bu">abs</span>(n)</span>
|
||||
<span id="cb2-5"><a href="#cb2-5"></a> <span class="cf">elif</span> n <span class="op">==</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb2-6"><a href="#cb2-6"></a> <span class="cf">return</span> <span class="bu">abs</span>(m)</span>
|
||||
<span id="cb2-7"><a href="#cb2-7"></a> <span class="cf">else</span>:</span>
|
||||
<span id="cb2-8"><a href="#cb2-8"></a> possible_divisors <span class="op">=</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="bu">min</span>(<span class="bu">abs</span>(m), <span class="bu">abs</span>(n)) <span class="op">+</span> <span class="dv">1</span>)</span>
|
||||
<span id="cb2-9"><a href="#cb2-9"></a> <span class="cf">return</span> <span class="bu">max</span>({d <span class="cf">for</span> d <span class="kw">in</span> possible_divisors <span class="cf">if</span> divides(d, m) <span class="kw">and</span> divides(d, n)})</span></code></pre></div>
|
||||
<h2 id="gcd-and-remainders">GCD and remainders</h2>
|
||||
<p>Here is the Quotient-Remainder Theorem we saw earlier in this chapter, slightly modified to allow for negative divisors as well.</p>
|
||||
<div id="Quotient-Remainder Theorem" class="theorem">
|
||||
<p>(Quotient-Remainder Theorem) For all <span class="math inline">\(n \in \Z\)</span> and <span class="math inline">\(d \in \Z\)</span>, if <span class="math inline">\(d \neq 0\)</span> then there exist <span class="math inline">\(q \in \Z\)</span> and <span class="math inline">\(r \in \N\)</span> such that <span class="math inline">\(n = qd + r\)</span> and <span class="math inline">\(0 \leq r < |d|\)</span>. Moreover, these <span class="math inline">\(q\)</span> and <span class="math inline">\(r\)</span> are <em>unique</em> for a given <span class="math inline">\(n\)</span> and <span class="math inline">\(d\)</span>.</p>
|
||||
<p>We say that <span class="math inline">\(q\)</span> is the <strong>quotient</strong> when <span class="math inline">\(n\)</span> is divided by <span class="math inline">\(d\)</span>, and that <span class="math inline">\(r\)</span> is the <strong>remainder</strong> when <span class="math inline">\(n\)</span> is divided by <span class="math inline">\(d\)</span>, and write <span class="math inline">\(r = n~\%~d\)</span>.</p>
|
||||
</div>
|
||||
<p>We can use this theorem to improve our algorithm by breaking down the problem into a smaller one. The key idea is the following theorem.</p>
|
||||
<div class="theorem">
|
||||
<p>For all <span class="math inline">\(a, b \in \Z\)</span> where <span class="math inline">\(b \neq 0\)</span>, <span class="math inline">\(\gcd(a, b) = \gcd(b, a~\%~b)\)</span>.</p>
|
||||
</div>
|
||||
<div class="translation">
|
||||
<p><span class="math inline">\(\forall a, b \in \Z,~ b \neq 0 \Rightarrow \gcd(a, b) = \gcd(b, a~\%~b)\)</span>.</p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>Before we try to prove this statement, let’s consider an example using the two numbers <span class="math inline">\(a = 24\)</span> and <span class="math inline">\(b = 16\)</span>. We know that <span class="math inline">\(\gcd(24, 16) = 8\)</span>. Also, the remainder when 24 is divided by 16 is 8, and <span class="math inline">\(\gcd(16, 8) = 8\)</span> as well.</p>
|
||||
<p>Before we get to a formal proof, let’s preview the main idea. We’ll define the variable <span class="math inline">\(d = \gcd(b, a~\%~b)\)</span>, and prove that <span class="math inline">\(d = \gcd(a, b)\)</span> as well. To do so, we’ll need to prove that <span class="math inline">\(d\)</span> divides both <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span>, and that it is greater than every other common divisor of <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span>. Watch for this structure in our actual proof below!</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(a, b \in \Z\)</span> and assume <span class="math inline">\(b \neq 0\)</span>. Also let <span class="math inline">\(r = a~\%~b\)</span> (the remainder when <span class="math inline">\(a\)</span> is divided by <span class="math inline">\(b\)</span>). We need to prove that <span class="math inline">\(\gcd(a, b) = \gcd(b, r)\)</span>.</p>
|
||||
<p>To do this, let <span class="math inline">\(d = \gcd(b, r)\)</span>. We’ll prove that <span class="math inline">\(d = \gcd(a, b)\)</span> as well, by proving three things: that <span class="math inline">\(d \mid a\)</span>, that <span class="math inline">\(d \mid b\)</span>, and that every common divisor of <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> is <span class="math inline">\(\leq d\)</span>.</p>
|
||||
<p><strong>Part 1</strong>: proving that <span class="math inline">\(d \mid a\)</span>.</p>
|
||||
<p>By our definition of <span class="math inline">\(r\)</span> and the Quotient-Remainder Theorem, we know that there exists <span class="math inline">\(q \in Z\)</span> such that <span class="math inline">\(a = qb + r\)</span>. Since <span class="math inline">\(d = \gcd(b, r)\)</span>, we know that <span class="math inline">\(d\)</span> divides both <span class="math inline">\(b\)</span> and <span class="math inline">\(r\)</span>. And so by the <a href="05-greatest-common-divisor.html##theorem:divide_lin_comb">Divisibility of Linear Combinations Theorem</a>, we know that <span class="math inline">\(d \mid qb + r\)</span>, and so <span class="math inline">\(d \mid a\)</span>.</p>
|
||||
<p><strong>Part 2</strong>: proving that <span class="math inline">\(d \mid b\)</span>.</p>
|
||||
<p>Since we defined <span class="math inline">\(d = \gcd(b, r)\)</span>, it must divide <span class="math inline">\(b\)</span> (by the definition of <span class="math inline">\(\gcd\)</span>).</p>
|
||||
<p><strong>Part 3</strong>: proving that every common divisor of <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> is <span class="math inline">\(\leq d\)</span>.</p>
|
||||
<p>Let <span class="math inline">\(d_1 \in \Z\)</span> and assume that <span class="math inline">\(d_1 \mid a\)</span> and <span class="math inline">\(d_1 \mid b\)</span>. We’ll prove that <span class="math inline">\(d_1 \leq d\)</span>.</p>
|
||||
<p>First, we’ll prove that <span class="math inline">\(d_1 \mid r\)</span>. We can rewrite the equation <span class="math inline">\(a = qb + r\)</span> (from the Quotient-Remainder Theorem) to obtain <span class="math inline">\(r = a - qb\)</span>. Then using our assumption that <span class="math inline">\(d_1\)</span> is a common divisor of <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> and <a href="05-greatest-common-divisor.html##theorem:divide_lin_comb">Divisibility of Linear Combinations Theorem</a> again, we can conclude that <span class="math inline">\(d_1 \mid r\)</span>.</p>
|
||||
<p>So then <span class="math inline">\(d_1 \mid b\)</span> (by our assumption), and <span class="math inline">\(d_1 \mid r\)</span>, and so it is a common divisor of <span class="math inline">\(b\)</span> and <span class="math inline">\(r\)</span>. Therefore by the definition of <span class="math inline">\(\gcd\)</span>, we know that <span class="math inline">\(d_1 \leq \gcd(b, r) = d\)</span>.</p>
|
||||
</div>
|
||||
<h2 id="gcd-remainders-and-a-new-algorithm">GCD, remainders, and a new algorithm</h2>
|
||||
<p>The theorem we just proved suggests a possible way of computing the gcd of two numbers in an iterative (repeated) fashion. Let’s again use 24 and 16 as our example.</p>
|
||||
<ul>
|
||||
<li>Since the remainder <span class="math inline">\(24~\%~16\)</span> is 8, we know that <span class="math inline">\(\gcd(24, 16) = \gcd(16, 8)\)</span>.</li>
|
||||
<li>Also, the remainder <span class="math inline">\(16~\%~8\)</span> is 0, and so we know that <span class="math inline">\(\gcd(16, 8) = \gcd(8, 0)\)</span>.</li>
|
||||
<li>But the gcd of any positive integer <span class="math inline">\(n\)</span> and 0 is simply <span class="math inline">\(n\)</span> itself,<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> Exercise: prove this using the definition of gcd!</span> and so we know <span class="math inline">\(\gcd(8, 0) = 8\)</span>.</li>
|
||||
<li>This tells use that <span class="math inline">\(\gcd(24, 16) = 8\)</span> as well!</li>
|
||||
</ul>
|
||||
<p>Let’s formalize this in a high-level description of an algorithm before we write the code. This algorithm for computing the gcd of two numbers is known as the <em>Euclidean algorithm</em>.<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote"> This is named after the Greek mathematician Euclid, although he originally developed the algorithm using subtraction (<span class="math inline">\(a - b\)</span>) rather than remainders (<span class="math inline">\(a~\%~b\)</span>).</span></p>
|
||||
<div class="framed">
|
||||
<p><strong>Euclidean Algorithm</strong></p>
|
||||
<p><em>Given</em>: integers <code>a</code> and <code>b</code>. <em>Returns</em>: <code>gcd(a, b)</code>.</p>
|
||||
<ol type="1">
|
||||
<li>Initialize two variables <code>x</code>, <code>y</code> to the given numbers <code>a</code> and <code>b</code>.</li>
|
||||
<li>Let <code>r</code> be the remainder when <code>x</code> is divided by <code>y</code>.</li>
|
||||
<li>Reassign <code>x</code> and <code>y</code> to <code>y</code> and <code>r</code>, respectively.</li>
|
||||
<li>Repeat steps 2 and 3 until <code>y</code> is 0.</li>
|
||||
<li>At this point, <code>x</code> refers to the gcd of <code>a</code> and <code>b</code>.</li>
|
||||
</ol>
|
||||
</div>
|
||||
<p>Here is how we can visualize the changing values of <code>x</code> and <code>y</code> for the given 24 and 6 in our previous example:<label for="sn-3" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-3" class="margin-toggle"/><span class="sidenote"> Note the similarity between this and the <em>loop accumulation tables</em> of Chapter 4.</span></p>
|
||||
<div class="reference-table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr class="header">
|
||||
<th style="text-align: center;">Iteration</th>
|
||||
<th style="text-align: center;"><code>x</code></th>
|
||||
<th style="text-align: center;"><code>y</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="odd">
|
||||
<td style="text-align: center;">0</td>
|
||||
<td style="text-align: center;">24</td>
|
||||
<td style="text-align: center;">16</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td style="text-align: center;">1</td>
|
||||
<td style="text-align: center;">16</td>
|
||||
<td style="text-align: center;">8</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td style="text-align: center;">2</td>
|
||||
<td style="text-align: center;">8</td>
|
||||
<td style="text-align: center;">0</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p>The main question for us in implementing this algorithm in Python is how we achieve step 4: repeating the two previous steps until some condition (“<code>y</code> is 0”) is satisfied. We know how to use for loops to iterate over a collection of values. This allowed us to repeat a sequence of statements (i.e., the body of the for loop) on every iteration. Naturally, the for loop ends when the statements have been repeated for all elements in a collection or range.</p>
|
||||
<p>But in the case of step 4, we would like to repeat code based on some condition: “Repeat steps 2 and 3 until the remainder is 0”. In these scenarios, we must use a different kind of loop in Python: the <strong>while loop</strong>.</p>
|
||||
<h2 id="the-while-loop">The while loop</h2>
|
||||
<p>A while loop looks very similar to an if statement:</p>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1"></a><span class="cf">while</span> <span class="op"><</span>condition<span class="op">></span>:</span>
|
||||
<span id="cb3-2"><a href="#cb3-2"></a> <span class="op"><</span>statement<span class="op">></span></span>
|
||||
<span id="cb3-3"><a href="#cb3-3"></a> ...</span></code></pre></div>
|
||||
<p>Unlike an if statement, after executing its body the while loop will check the condition again. If the condition still evaluates to <code>True</code>, then the body is repeated. Let’s try an example:</p>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1"></a><span class="op">>>></span> numbers <span class="op">=</span> []</span>
|
||||
<span id="cb4-2"><a href="#cb4-2"></a><span class="op">>>></span> number <span class="op">=</span> <span class="dv">1</span></span>
|
||||
<span id="cb4-3"><a href="#cb4-3"></a><span class="op">>>></span> <span class="cf">while</span> number <span class="op"><</span> <span class="dv">100</span>:</span>
|
||||
<span id="cb4-4"><a href="#cb4-4"></a>... numbers.append(number)</span>
|
||||
<span id="cb4-5"><a href="#cb4-5"></a>... number <span class="op">=</span> number <span class="op">*</span> <span class="dv">2</span></span>
|
||||
<span id="cb4-6"><a href="#cb4-6"></a>...</span>
|
||||
<span id="cb4-7"><a href="#cb4-7"></a><span class="op">>>></span> numbers</span>
|
||||
<span id="cb4-8"><a href="#cb4-8"></a>[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">16</span>, <span class="dv">32</span>, <span class="dv">64</span>]</span></code></pre></div>
|
||||
<p>Notice how <code>number</code> appears in both the while loop’s body and its condition. In the loop body, <code>number</code> is increasing at each iteration (we accumulated the values in the list <code>numbers</code>). Eventually, <code>number</code> refers to the value <code>128</code> and the while loop is done because <code>128 < 100</code> evaluates to <code>False</code>. Note that the number of iterations of our while loop is dependent on the initial value of <code>number</code>. Had we started with a value of, for example, <code>10</code>, the loop would have only 4 iterations (not 6, as when <code>number</code> started with <code>2</code>). Similarly, if <code>number</code> was initially some value greater than or equal to <code>100</code>, then the while loop would never have executed its body (just as a for loop does not execute its body if given an empty collection).</p>
|
||||
<h2 id="implementing-the-euclidean-algorithm">Implementing the Euclidean Algorithm</h2>
|
||||
<p>Here is our (first) implementation of the Euclidean algorithm for computing the gcd of two numbers.</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1"></a><span class="kw">def</span> euclidean_gcd(a: <span class="bu">int</span>, b: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">int</span>:</span>
|
||||
<span id="cb5-2"><a href="#cb5-2"></a> <span class="co">"""Return the gcd of a and b."""</span></span>
|
||||
<span id="cb5-3"><a href="#cb5-3"></a> <span class="co"># Step 1: initialize x and y</span></span>
|
||||
<span id="cb5-4"><a href="#cb5-4"></a> x <span class="op">=</span> a</span>
|
||||
<span id="cb5-5"><a href="#cb5-5"></a> y <span class="op">=</span> b</span>
|
||||
<span id="cb5-6"><a href="#cb5-6"></a> <span class="cf">while</span> y <span class="op">!=</span> <span class="dv">0</span>: <span class="co"># Step 4: repeat Steps 2 and 3 until y is 0</span></span>
|
||||
<span id="cb5-7"><a href="#cb5-7"></a> <span class="co"># Step 2: calculate the remainder of x divided by y</span></span>
|
||||
<span id="cb5-8"><a href="#cb5-8"></a> r <span class="op">=</span> x <span class="op">%</span> y</span>
|
||||
<span id="cb5-9"><a href="#cb5-9"></a></span>
|
||||
<span id="cb5-10"><a href="#cb5-10"></a> <span class="co"># Step 3: reassign x and y</span></span>
|
||||
<span id="cb5-11"><a href="#cb5-11"></a> x <span class="op">=</span> y</span>
|
||||
<span id="cb5-12"><a href="#cb5-12"></a> y <span class="op">=</span> r</span>
|
||||
<span id="cb5-13"><a href="#cb5-13"></a></span>
|
||||
<span id="cb5-14"><a href="#cb5-14"></a> <span class="co"># Step 5: x now refers to the gcd of a and b</span></span>
|
||||
<span id="cb5-15"><a href="#cb5-15"></a> <span class="cf">return</span> x</span></code></pre></div>
|
||||
<p>How does this loop work? To understand it better, let’s see how this maps onto our original algorithm.</p>
|
||||
<ul>
|
||||
<li>Step 1, initializing <code>x</code> and <code>y</code>, occurs in the code before the while loop begins.</li>
|
||||
<li>Steps 2 and 3 are performed inside the loop body.</li>
|
||||
<li>Step 4, the repetition, is achieved by the while loop. One subtlety is that our original algorithm specified a <em>stopping condition</em>, “repeat until X”. When writing Python <code>while</code> loops, however, we must write a <em>continuing condition</em>, which is the negation of the stopping condition. So “until <span class="math inline">\(y = 0\)</span>” becomes <code>while y != 0</code>.</li>
|
||||
<li>Step 5, the return value, is exactly what is specified by the algorithm.</li>
|
||||
</ul>
|
||||
<p>Let’s see an example trace of the <code>euclidean_gcd</code> loop for the sample call <code>euclidean_gcd(24, 16)</code>:</p>
|
||||
<div class="reference-table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr class="header">
|
||||
<th style="text-align: center;">Iteration</th>
|
||||
<th style="text-align: center;"><code>x</code></th>
|
||||
<th style="text-align: center;"><code>y</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="odd">
|
||||
<td style="text-align: center;">0</td>
|
||||
<td style="text-align: center;">24</td>
|
||||
<td style="text-align: center;">16</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td style="text-align: center;">1</td>
|
||||
<td style="text-align: center;">16</td>
|
||||
<td style="text-align: center;">8</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td style="text-align: center;">2</td>
|
||||
<td style="text-align: center;">8</td>
|
||||
<td style="text-align: center;">0</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p>In our implementation, we don’t have a typical accumulator pattern. Instead, both <code>x</code> and <code>y</code> are <em>loop variables</em> for the while loop, which illustrates one major difference between while loops and for loops. In a for loop, the loop variable is initialized and reassigned automatically by the Python interpreter to each element of the collection being looped over. In a while loop, the loop variable(s) must be initialized and reassigned explicitly in code that we write.</p>
|
||||
<p>This difference makes while loops more flexible than for loops, as the programmer has full control over exactly how the loop variable changes. This is both a strength and a weakness! While loops can be used to express algorithms that are cumbersome or impossible to express with for loops, but at the cost of requiring the programmer to write more code to keep track of loop variables.<label for="sn-4" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-4" class="margin-toggle"/><span class="sidenote"> Remember: the more code you write, the more potential there is for error.</span> So a good rule of thumb is to use for loops where possible (when you have an explicit collection to loop over), and reserve while loops for situations that can’t be easily implemented with a for loop.</p>
|
||||
<h3 id="parallel-assignment">Parallel assignment</h3>
|
||||
<p>One subtlety of our loop body is the order in which the loop variables are updated. Suppose we had swapped the last two lines of the loop body:</p>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1"></a> <span class="cf">while</span> y <span class="op">!=</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb6-2"><a href="#cb6-2"></a> r <span class="op">=</span> x <span class="op">%</span> y</span>
|
||||
<span id="cb6-3"><a href="#cb6-3"></a> y <span class="op">=</span> r</span>
|
||||
<span id="cb6-4"><a href="#cb6-4"></a> x <span class="op">=</span> y</span></code></pre></div>
|
||||
<p>This is a really easy change to make, but also incorrect: because the statement <code>y = r</code> is executed first, the next statement <code>x = y</code> assigns <code>x</code> to the <em>new</em> value of <code>y</code> rather than its old one!</p>
|
||||
<p>When performing reassignment of multiple variables, where the new variable values depend on the old ones, it is important to keep track of the reassignment order so that you don’t accidentally lose previous variable values. To avoid this problem altogether, Python has a neat feature called <em>parallel assignment</em>, in which multiple variables can be assigned in the same statment.</p>
|
||||
<p>Here is how we can rewrite the loop body using parallel assignment:</p>
|
||||
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1"></a> <span class="cf">while</span> y <span class="op">!=</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb7-2"><a href="#cb7-2"></a> r <span class="op">=</span> x <span class="op">%</span> y</span>
|
||||
<span id="cb7-3"><a href="#cb7-3"></a> x, y <span class="op">=</span> y, r</span></code></pre></div>
|
||||
<p>The assignment statement <code>x, y = y, r</code> is evaluated as follows:</p>
|
||||
<ul>
|
||||
<li>First, the right-hand side <code>y, r</code> is evaluated, producing two objects.<label for="sn-5" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-5" class="margin-toggle"/><span class="sidenote">Or more precisely, the <em>ids</em> of two objects.</span></li>
|
||||
<li>Then, each object is assigned to the corresponding variable on the left-hand side.</li>
|
||||
</ul>
|
||||
<p>In parallel assignment, the right-hand side is fully evaluated before any variable reassignment occurs. This means that the assignment statement <code>x, y = y, r</code> has the <em>same effect</em> as <code>y, x = r, y</code>—order doesn’t matter, and so we can think of each variable assignment happening in parallel, without one affecting the other.</p>
|
||||
<p>Parallel is a very useful tool when reassigning variables, so please take advantage of it to help simplify your code and avoid the “update order” problem of variable reassignment. Here is how we can rewrite the <code>euclidean_gcd</code> using parallel assignment:</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1"></a><span class="kw">def</span> euclidean_gcd(a: <span class="bu">int</span>, b: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">int</span>:</span>
|
||||
<span id="cb8-2"><a href="#cb8-2"></a> <span class="co">"""Return the gcd of a and b."""</span></span>
|
||||
<span id="cb8-3"><a href="#cb8-3"></a> x, y <span class="op">=</span> a, b</span>
|
||||
<span id="cb8-4"><a href="#cb8-4"></a> <span class="cf">while</span> y <span class="op">!=</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb8-5"><a href="#cb8-5"></a> r <span class="op">=</span> x <span class="op">%</span> y</span>
|
||||
<span id="cb8-6"><a href="#cb8-6"></a> x, y <span class="op">=</span> y, r</span>
|
||||
<span id="cb8-7"><a href="#cb8-7"></a></span>
|
||||
<span id="cb8-8"><a href="#cb8-8"></a> <span class="cf">return</span> x</span></code></pre></div>
|
||||
<h3 id="documenting-loop-properties-loop-invariants">Documenting loop properties: loop invariants</h3>
|
||||
<p>Our implementation of <code>euclidean_gcd</code> doesn’t follow a typical pattern of code we’ve seen so far. If we didn’t know anything about the algorithm and were simply looking at the code, it would be quite mysterious why it works. To improve the readability of this code, we want some way of documenting what we know about the loop variables <code>x</code> and <code>y</code> inside the loop body.</p>
|
||||
<p>Recall that the Euclidean Algorithm relies on one key property, that <code>gcd(x, y) == gcd(y, x % y)</code>. At each loop iteration, <code>x</code> and <code>y</code> are updated so that <code>x = y</code> and <code>y = x % y</code>. The key property that we want to capture is that <em>even though <code>x</code> and <code>y</code> change, their gcd doesn’t</em>. Since <code>x</code> and <code>y</code> are initialized to <code>a</code> and <code>b</code>, another way to express this is that at every loop iteration, <code>gcd(x, y) == gcd(a, b)</code>. We call this statement a <strong>loop invariant</strong>, which is a property about loop variables that must be true at the start and end of each loop iteration.<label for="sn-6" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-6" class="margin-toggle"/><span class="sidenote"> This is similar to representation invariants, which are properties of instance attributes that must be true for every instance of a given data class.</span></p>
|
||||
<p>By convention, we document loop invariants at the top of a loop body using an assert statement.</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1"></a><span class="kw">def</span> euclidean_gcd(a: <span class="bu">int</span>, b: <span class="bu">int</span>) <span class="op">-></span> <span class="bu">int</span>:</span>
|
||||
<span id="cb9-2"><a href="#cb9-2"></a> <span class="co">"""Return the gcd of a and b."""</span></span>
|
||||
<span id="cb9-3"><a href="#cb9-3"></a> x, y <span class="op">=</span> a, b</span>
|
||||
<span id="cb9-4"><a href="#cb9-4"></a></span>
|
||||
<span id="cb9-5"><a href="#cb9-5"></a> <span class="cf">while</span> y <span class="op">!=</span> <span class="dv">0</span>:</span>
|
||||
<span id="cb9-6"><a href="#cb9-6"></a> <span class="co"># Loop invariant (we use naive_gcd to check that the gcd are correct)</span></span>
|
||||
<span id="cb9-7"><a href="#cb9-7"></a> <span class="cf">assert</span> naive_gcd(x, y) <span class="op">==</span> naive_gcd(a, b)</span>
|
||||
<span id="cb9-8"><a href="#cb9-8"></a></span>
|
||||
<span id="cb9-9"><a href="#cb9-9"></a> r <span class="op">=</span> x <span class="op">%</span> y</span>
|
||||
<span id="cb9-10"><a href="#cb9-10"></a> x, y <span class="op">=</span> y, r</span>
|
||||
<span id="cb9-11"><a href="#cb9-11"></a></span>
|
||||
<span id="cb9-12"><a href="#cb9-12"></a> <span class="cf">return</span> x</span></code></pre></div>
|
||||
<p>Because this loop invariant must be true at the start and end of each loop iteration, it is also true after the loop stops (i.e., when <code>y == 0</code>). In this case, the loop invariant tells us that <code>gcd(x, 0) == gcd(a, b)</code>, and so we know that <code>x == gcd(a, b)</code>, which is why <code>x</code> is returned.</p>
|
||||
<p>Loop invariants are a powerful way to document properties of our code, to better enable us to reason about our code. But remember that loop invariants by themselves are just statements; the only way to know for sure whether a loop invariant is correct is to do a proof, much like the one we did at the beginning of this section.</p>
|
||||
<!-- ## The Extended Euclidean Algorithm
|
||||
|
||||
Recall the *GCD characterization theorem* from the previous section, which said that $\gcd(a, b)$ is the smallest positive integer that can be written as a linear combination of $a$ and $b$.
|
||||
|
||||
How would we go about proving such a statement?
|
||||
Our previous proofs of existential statements were all *constructive*: we pick values for the existentially-quantified variables and show that those variables satisfy the rest of the statement we're trying to prove.
|
||||
|
||||
First, we're going to see a non-constructive proof: a proof that shows the existence of values, but doesn't necessarily give explicit expressions for them.
|
||||
|
||||
<div class="proof">
|
||||
Let $x, y \in Z$, and assume $x \neq 0$ or $y \neq 0$.
|
||||
We define the set $S = \{ px + qy \mid p, q \in \Z \land px + qy > 0 \}$,
|
||||
and let $d = \min(S)$.
|
||||
We'll prove that $d = \gcd(x, y)$.
|
||||
This proof uses a new technique to show equality: first, we show $d \geq \gcd(x, y)$, and then $d \leq \gcd(x, y)$, so that the only possibility is that $d$ in fact equals $\gcd(x, y)$.
|
||||
|
||||
**Part 1**: proving that $d \geq \gcd(x, y)$.
|
||||
|
||||
Since $\gcd(x, y)$ divides both $x$ and $y$, by Theorem TODO, we know that it divides any linear combination of $x$ and $y$.
|
||||
And so $\gcd(x, y) \mid d$; since both numbers are positive, this implies that $\gcd(x, y) \leq d$.
|
||||
|
||||
**Part 2**: proving that $d \leq \gcd(x, y)$.
|
||||
|
||||
First, we'll prove that $d \mid x$.
|
||||
By the Quotient-Remainder Theorem, we write $x = dq + r$, where $q, r \in Z$ and $0 \leq r < d$.
|
||||
We can rearrange this equation to obtain $r = x - dq$, which means $r$ is a linear combination of $x$ and $y$ (since $d$ is a linear combination of $x$ and $y$).
|
||||
But since we know $r < d$ and $d = \min(S)$, this implies $r \notin S$, which is only possible when $r = 0$.
|
||||
So $d \mid x$.
|
||||
|
||||
Similarly, $d \mid y$, and so $d$ is a common divisor of $x$ and $y$.
|
||||
By the definition of $\gcd$, we can conclude that $d \leq \gcd(x, y)$.
|
||||
</div>
|
||||
|
||||
This proof is technically correct, and interesting because it shows a new technique for proving existentials.
|
||||
But it is also a bit unsatisfying in that it proves the existence of some values (coefficients of the linear combination) without showing how to find them.
|
||||
It turns out that a variation of the Euclidean Algorithm called the *Extended Euclidean Algorithm* can be used to not just find the gcd of two numbers, but find the coefficients of the linear combination that equals that gcd.
|
||||
|
||||
You'll explore this more on an exercise a bit later in this course.
|
||||
|
||||
</section> -->
|
||||
<!--
|
||||
|
||||
Recall that the implementation relies on a *property* of divisibility to restrict the set of numbers to quantify over:
|
||||
when $n \neq 0$, every number that divides $n$ must lie in the range ${- |n|, -|n| + 1, \dots, |n| - 1, |n|}$.
|
||||
We can use `divides` in another function that searches for the gcd.
|
||||
In this scenario, it makes sense to start from the end of the `possible_divisors` range and work our way backwards.
|
||||
For example, for two numbers `x = 16` and `y = 24`:
|
||||
|
||||
```python
|
||||
>>> x = 16
|
||||
>>> y = 24
|
||||
>>> range(min(abs(y), abs(x)), 0, -1)
|
||||
range(16, 0, -1)
|
||||
>>> list(range(min(abs(y), abs(x)), 0, -1))
|
||||
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
||||
```
|
||||
|
||||
In this scenario, we asked range to start at a value greater than its stop (i.e., `16` > `0`, recall that the stop is not included).
|
||||
In addition, we provided range with a step value of `-1`.
|
||||
We can now test if, for each `d` in the range above, `d` divides evenly into both `x` and `y`:
|
||||
|
||||
```python
|
||||
def gcd(x: int, y: int) -> int:
|
||||
"""Return the greatest common divisor of x and y."""
|
||||
for d in range(min(abs(y), abs(x)), 0, -1):
|
||||
if divides(d, x) and divides(d, y):
|
||||
return d
|
||||
|
||||
return 0
|
||||
```
|
||||
-->
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,379 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||||
<title>6.7 Modular Arithmetic</title>
|
||||
<style>
|
||||
code{white-space: pre-wrap;}
|
||||
span.smallcaps{font-variant: small-caps;}
|
||||
span.underline{text-decoration: underline;}
|
||||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||||
ul.task-list{list-style: none;}
|
||||
</style>
|
||||
<link rel="stylesheet" href="../tufte.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" type="text/javascript"></script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div style="display:none">
|
||||
\(
|
||||
\newcommand{\NOT}{\neg}
|
||||
\newcommand{\AND}{\wedge}
|
||||
\newcommand{\OR}{\vee}
|
||||
\newcommand{\XOR}{\oplus}
|
||||
\newcommand{\IMP}{\Rightarrow}
|
||||
\newcommand{\IFF}{\Leftrightarrow}
|
||||
\newcommand{\TRUE}{\text{True}\xspace}
|
||||
\newcommand{\FALSE}{\text{False}\xspace}
|
||||
\newcommand{\IN}{\,{\in}\,}
|
||||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||||
\newcommand{\TO}{\rightarrow}
|
||||
\newcommand{\DIV}{\mid}
|
||||
\newcommand{\NDIV}{\nmid}
|
||||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||||
\newcommand{\N}{\mathbb N}
|
||||
\newcommand{\Z}{\mathbb Z}
|
||||
\newcommand{\Q}{\mathbb Q}
|
||||
\newcommand{\R}{\mathbb R}
|
||||
\newcommand{\C}{\mathbb C}
|
||||
\newcommand{\cA}{\mathcal A}
|
||||
\newcommand{\cB}{\mathcal B}
|
||||
\newcommand{\cC}{\mathcal C}
|
||||
\newcommand{\cD}{\mathcal D}
|
||||
\newcommand{\cE}{\mathcal E}
|
||||
\newcommand{\cF}{\mathcal F}
|
||||
\newcommand{\cG}{\mathcal G}
|
||||
\newcommand{\cH}{\mathcal H}
|
||||
\newcommand{\cI}{\mathcal I}
|
||||
\newcommand{\cJ}{\mathcal J}
|
||||
\newcommand{\cL}{\mathcal L}
|
||||
\newcommand{\cK}{\mathcal K}
|
||||
\newcommand{\cN}{\mathcal N}
|
||||
\newcommand{\cO}{\mathcal O}
|
||||
\newcommand{\cP}{\mathcal P}
|
||||
\newcommand{\cQ}{\mathcal Q}
|
||||
\newcommand{\cS}{\mathcal S}
|
||||
\newcommand{\cT}{\mathcal T}
|
||||
\newcommand{\cV}{\mathcal V}
|
||||
\newcommand{\cW}{\mathcal W}
|
||||
\newcommand{\cZ}{\mathcal Z}
|
||||
\newcommand{\emp}{\emptyset}
|
||||
\newcommand{\bs}{\backslash}
|
||||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||||
\newcommand{\xspace}{}
|
||||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||||
\)
|
||||
</div>
|
||||
<header id="title-block-header">
|
||||
<h1 class="title">6.7 Modular Arithmetic</h1>
|
||||
</header>
|
||||
<section>
|
||||
<p>In this section, we’ll explore some properties of modular arithmetic that will be useful in the next chapter, when we study cryptographic algorithms based on modular arithmetic. First, recall the definition of modular equivalence from <a href="01-number-theory-definitions.html">6.1 An Introduction to Number Theory</a>.</p>
|
||||
<div class="definition">
|
||||
<p>Let <span class="math inline">\(a, b, n \in \Z\)</span>, and assume <span class="math inline">\(n \neq 0\)</span>. We say that <strong><span class="math inline">\(a\)</span> is equivalent to <span class="math inline">\(b\)</span> modulo <span class="math inline">\(n\)</span></strong> when <span class="math inline">\(n \mid a - b\)</span>. In this case, we write <span class="math inline">\(a \equiv b \pmod n\)</span>.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">One warning: the notation <span class="math inline">\(a \equiv b \pmod n\)</span> is not exactly the same as <code>mod</code> or <code>%</code> operator you are familiar with from programming; here, both <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> could be much larger than <span class="math inline">\(n\)</span>, or even negative.</span></p>
|
||||
</div>
|
||||
<p>This definition captures the idea that <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> have the <em>same remainder</em> when divided by <span class="math inline">\(n\)</span>. You should think of this congruence relation as being analogous to numeric equality, with a relaxation. When we write <span class="math inline">\(a = b\)</span>, we mean that the numeric values of <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> are literally equal. When we write <span class="math inline">\(a \equiv b \pmod n\)</span>, we we mean that if you look at the remainders of <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> when divided by <span class="math inline">\(n\)</span>, those remainders are literally equal.</p>
|
||||
<p>We will next look at how addition, subtraction, and multiplication all behave in an analogous fashion under modular arithmetic. The following proof is a little tedious because it is calculation-heavy; the main benefits here are practicing reading and using a new definition, and getting comfortable with this particular notation.</p>
|
||||
<div class="theorem">
|
||||
<p>For all <span class="math inline">\(a, b, c, d, n \in \Z\)</span>, if <span class="math inline">\(n \neq 0\)</span>, if <span class="math inline">\(a \equiv c \pmod n\)</span> and <span class="math inline">\(b \equiv d \pmod n\)</span>, then:</p>
|
||||
<ol type="1">
|
||||
<li><span class="math inline">\(a + b \equiv c + d \pmod n\)</span></li>
|
||||
<li><span class="math inline">\(a - b \equiv c - d \pmod n\)</span></li>
|
||||
<li><span class="math inline">\(ab \equiv cd \pmod n\)</span></li>
|
||||
</ol>
|
||||
<div class="translation">
|
||||
<p>We will only show how to translate and prove (2), and leave (1) and (3) as exercises. <span class="math display">\[\forall a, b, c, d, n \in \Z,~ \big(n \neq 0 \AND (n \DIV a - c) \AND (n \DIV b - d) \big) \IMP n \DIV (a - b) - (c - d).\]</span></p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(a, b, c, d, n \in \Z\)</span>. Assume that <span class="math inline">\(n \neq 0\)</span>, <span class="math inline">\(n \DIV a - c\)</span>, and <span class="math inline">\(n \DIV b - d\)</span>. This means we want to prove that <span class="math inline">\(n \DIV (a-c) - (b-d).\)</span></p>
|
||||
<p>By the <a href="05-greatest-common-divisor.html#theorem:divide_lin_comb">Divisibility of Linear Combinations Theorem</a>, since <span class="math inline">\(n \DIV (a-c)\)</span> and <span class="math inline">\(n \DIV (b - d)\)</span>, it divides their difference:</p>
|
||||
<p><span class="math display">\[\begin{align*}
|
||||
n &\DIV (a-c) - (b-d) \\
|
||||
n &\DIV (a-b) - (c-d)
|
||||
\end{align*}\]</span></p>
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="modular-division">Modular division</h2>
|
||||
<p>The above example stated that addition, subtraction, and multiples all preserve modular equivalence—but what above division? The following statement is a “divide by <span class="math inline">\(k\)</span>” property, but is actually <strong>False</strong>:<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> A good exercise is to disprove this statement!</span> <span class="math display">\[
|
||||
\forall a, b, k, n \in \Z,~ n > 0 \land ak \equiv bk \pmod n \Rightarrow a \equiv b \pmod n
|
||||
\]</span></p>
|
||||
<p>For the real numbers, division <span class="math inline">\(\frac{x}{y}\)</span> has a single gap: when <span class="math inline">\(y = 0\)</span>. As we’ll see in the next theorem, division in modular arithmetic can have many such gaps, but we can also predict exactly where these gaps will occur.</p>
|
||||
<div id="theorem:modular-inverse" class="theorem" data-label="Modular inverse">
|
||||
<p>(<em>Modular inverse</em>) Let <span class="math inline">\(n \in \Z^+\)</span> and <span class="math inline">\(a \in \Z\)</span>. If <span class="math inline">\(\gcd(a, n) = 1\)</span>, then there exists <span class="math inline">\(p \in \Z\)</span> such that <span class="math inline">\(ap \equiv 1 \pmod n\)</span>.</p>
|
||||
<p>We call this <span class="math inline">\(p\)</span> a <strong>modular inverse of <span class="math inline">\(a\)</span> modulo <span class="math inline">\(n\)</span></strong>.</p>
|
||||
</div>
|
||||
<div class="translation">
|
||||
<p><span class="math inline">\(\forall n \in \Z^+, \forall a \in \Z,~ \gcd(a, n) = 1 \Rightarrow \big(\exists p \in \Z,~ ap \equiv 1 \pmod n \big)\)</span></p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(n \in \Z^+\)</span> and <span class="math inline">\(a \in Z\)</span>. Assume <span class="math inline">\(\gcd(a, n) = 1\)</span>.</p>
|
||||
<p>Since <span class="math inline">\(\gcd(a, n) = 1\)</span>, by the <a href="05-greatest-common-divisor.html#theorem:gcd_characterization">GCD Characterization Theorem</a> we know that there exist integers <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span> such that <span class="math inline">\(pa + qn = \gcd(a, n) = 1\)</span>.</p>
|
||||
<p>Rearranging this equation, we get that <span class="math inline">\(pa - 1 = qn\)</span>, and so (by the definition of divisibility, taking <span class="math inline">\(k = q\)</span>), <span class="math inline">\(n \mid pa - 1\)</span>.</p>
|
||||
<p>Then by the definition of modular equivalence, <span class="math inline">\(pa \equiv 1 \pmod n\)</span>.</p>
|
||||
</div>
|
||||
<p>From this theorem about modular inverses, we can build up a form of division for modular arithmetic. To gain some intuition, first think about division <span class="math inline">\(\frac{a}{b}\)</span> as the <em>solution</em> to an equation of the form <span class="math inline">\(ax = b\)</span>. We’ll turn this into a statement about modular equivalence now.</p>
|
||||
<div class="example">
|
||||
<p>Let <span class="math inline">\(a \in \Z\)</span> and <span class="math inline">\(n \in \Z^+\)</span>. If <span class="math inline">\(\gcd(a, n) = 1\)</span>, then for all <span class="math inline">\(b \in \Z\)</span>, there exists <span class="math inline">\(k \in \Z\)</span> such that <span class="math inline">\(ak \equiv b \pmod n\)</span>.</p>
|
||||
<div class="translation">
|
||||
<p>This statement is quite complex! Remember that we focus on translation to examine the structure of the statement, so that we know how to set up a proof. We aren’t going to expand every single definition for the sake of expanding definitions.</p>
|
||||
<p><span class="math display">\[\forall n \in \Z^+, \forall a \in \Z,~ \gcd(a, n) = 1 \Rightarrow
|
||||
\big(\forall b \in \Z,~ \exists k \in \Z,~ ak \equiv b \pmod n \big).\]</span></p>
|
||||
</div>
|
||||
<div class="discussion">
|
||||
<p>So this is saying that under the given assumptions, <span class="math inline">\(b\)</span> is “divisible” by <span class="math inline">\(a\)</span> modulo <span class="math inline">\(n\)</span>. This comes after the theorem about modular inverses, so that should be useful. The conclusion is “there exists a <span class="math inline">\(k \in \Z\)</span> such that…” so that I know that at some point I’ll need to define a variable <span class="math inline">\(k\)</span> in terms of <span class="math inline">\(a\)</span>, <span class="math inline">\(b\)</span>, and/or <span class="math inline">\(n\)</span>, which satisfies the congruence.</p>
|
||||
<p>I notice that the hypothesis here (<span class="math inline">\(\gcd(a, n) = 1\)</span>) matches with the hypothesis from the previous theorem, so that seems to be something I can use. That gives me a <span class="math inline">\(p \in \Z\)</span> such that <span class="math inline">\(ap \equiv 1 \pmod n\)</span>…</p>
|
||||
<p>Wait, I can multiply both sides by <span class="math inline">\(b\)</span>, right?!</p>
|
||||
</div>
|
||||
<div class="proof">
|
||||
<p>Let <span class="math inline">\(a \in \Z\)</span> and <span class="math inline">\(n \in \Z^+\)</span>. Assume <span class="math inline">\(\gcd(a, n) = 1\)</span>, and let <span class="math inline">\(b \in \Z\)</span>. We want to prove that there exists <span class="math inline">\(k \in \Z\)</span> such that <span class="math inline">\(ak \equiv b \pmod n\)</span>.</p>
|
||||
<p>First, using the previous <em>Modular Inverses</em> theorem, since we assmed <span class="math inline">\(\gcd(a, n) = 1\)</span>, we know that there exists <span class="math inline">\(p \in \Z\)</span> such that <span class="math inline">\(ap \equiv 1 \pmod n\)</span>.</p>
|
||||
<p>Second, we know from (3) of our first example above that we can modular equivalence preserves multiplication, and so we know <span class="math inline">\(apb \equiv b \pmod n\)</span>.</p>
|
||||
<p>Then we let <span class="math inline">\(k = pb\)</span>, and we have that <span class="math inline">\(ak \equiv b \pmod n\)</span>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<p>These two theorems bring together elements from all of our study of proofs so far. We have both types of quantifiers, mixed with a larger implication. We used the <a href="05-greatest-common-divisor.html#theorem:gcd_characterization">GCD Characterization Theorem</a> for a key step in our proof. This illustrates the power of separating ideas into different statements and using each one to prove the next, just like we separate code into different functions in our programs!</p>
|
||||
<h2 id="exponentiation-and-order">Exponentiation and order</h2>
|
||||
<p>The last ingredient we’ll need to understand for our study of cryptography next week is the patterns that emerge when it comes to exponentiation in modular arithmetic. In normal arithmetic, powers of positive integers increase without bound, but in modular arithmetic we can focus on the <em>remainders</em> of powers, and discover some wonderful properties. For example, <span class="math inline">\(10^{13}\)</span> is a very large number indeed, but <span class="math inline">\(10^{13} \equiv 3 \pmod 7\)</span>! In fact, because there are only a finite number of remainders for any given <span class="math inline">\(n \in \Z^+\)</span>, for any <span class="math inline">\(a \in \Z\)</span> the infinite sequence of <em>remainders</em> of <span class="math inline">\(a^0\)</span>, <span class="math inline">\(a^1\)</span>, <span class="math inline">\(a^2\)</span>, <span class="math inline">\(a^3\)</span>, <span class="math inline">\(\dots\)</span> must repeat at some point.</p>
|
||||
<p>For example, let’s see what happens for each of the possible bases modulo 7:<label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote"> Because exponentiation by positive integers corresponds to repeated multiplication, which behaves “nicely” with modular arithmetic, the list below covers all possible integers. For example, because <span class="math inline">\(10 \equiv 3 \pmod 7\)</span>, we also know that <span class="math inline">\(10^{13} \equiv 3^{13} \pmod 7\)</span>.</span></p>
|
||||
<ul>
|
||||
<li>0: <span class="math inline">\(0^1 \equiv 0 \pmod 7\)</span>, <span class="math inline">\(0^2 \equiv 0 \pmod 7\)</span></li>
|
||||
<li>1: <span class="math inline">\(1^1 \equiv 1 \pmod 7\)</span>, <span class="math inline">\(1^2 \equiv 1 \pmod 7\)</span></li>
|
||||
<li>2: <span class="math inline">\(2^1 \equiv 2 \pmod 7\)</span>, <span class="math inline">\(2^2 \equiv 4 \pmod 7\)</span>, <span class="math inline">\(2^3 \equiv 1 \pmod 7\)</span>, <span class="math inline">\(2^4 \equiv 2 \pmod 7\)</span></li>
|
||||
<li>3: <span class="math inline">\(3^1 \equiv 3 \pmod 7\)</span>, <span class="math inline">\(3^2 \equiv 2 \pmod 7\)</span>, <span class="math inline">\(3^3 \equiv 6 \pmod 7\)</span>, <span class="math inline">\(3^4 \equiv 4 \pmod 7\)</span>, <span class="math inline">\(3^5 \equiv 5 \pmod 7\)</span>, <span class="math inline">\(3^6 \equiv 1 \pmod 7\)</span>, <span class="math inline">\(3^7 \equiv 3 \pmod 7\)</span></li>
|
||||
<li>4: <span class="math inline">\(4^1 \equiv 4 \pmod 7\)</span>, <span class="math inline">\(4^2 \equiv 2 \pmod 7\)</span>, <span class="math inline">\(4^3 \equiv 1 \pmod 7\)</span>, <span class="math inline">\(4^4 \equiv 4 \pmod 7\)</span></li>
|
||||
<li>5: <span class="math inline">\(5^1 \equiv 5 \pmod 7\)</span>, <span class="math inline">\(5^2 \equiv 4 \pmod 7\)</span>, <span class="math inline">\(5^3 \equiv 6 \pmod 7\)</span>, <span class="math inline">\(5^4 \equiv 2 \pmod 7\)</span>, <span class="math inline">\(5^5 \equiv 3 \pmod 7\)</span>, <span class="math inline">\(5^6 \equiv 1 \pmod 7\)</span>, <span class="math inline">\(5^7 \equiv 5 \pmod 7\)</span></li>
|
||||
<li>6: <span class="math inline">\(6^1 \equiv 6 \pmod 7\)</span>, <span class="math inline">\(6^2 \equiv 1 \pmod 7\)</span>, <span class="math inline">\(6^3 \equiv 6 \pmod 7\)</span></li>
|
||||
</ul>
|
||||
<p>No matter which base we start with, we enter a cycle. For example, the cycle starting with 2 is <span class="math inline">\([2, 4, 1, 2, \dots]\)</span>. We say this cycle has length 3, since it takes three elements in the sequence for the 2 to repeat. Here are the cycle lengths for each possible <span class="math inline">\(a \in \{0, 1, \dots, 6\}\)</span>:</p>
|
||||
<table>
|
||||
<thead>
|
||||
<tr class="header">
|
||||
<th><span class="math inline">\(a\)</span></th>
|
||||
<th>Cycle length</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="odd">
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>2</td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>3</td>
|
||||
<td>6</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>4</td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>5</td>
|
||||
<td>6</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>6</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>For each base other than 0, there is another way of looking at the cycle length: the cycle length for base <span class="math inline">\(a\)</span> is the smallest positive integer <span class="math inline">\(k\)</span> such that <span class="math inline">\(a^k \equiv 1 \pmod 7\)</span>. For example, <span class="math inline">\(2^3 \equiv 1 \pmod 7\)</span>, and the cycle repeats at <span class="math inline">\(2^4 \equiv 2^3 \cdot 2 \equiv 2 \pmod 7\)</span>.</p>
|
||||
<p>This “cycle length” is a fundamental property of modular exponentiation, and warrants its own definition.</p>
|
||||
<div class="definition">
|
||||
<p>Let <span class="math inline">\(a \in \Z\)</span> and <span class="math inline">\(n \in \Z^+\)</span>. We define the <strong>order of <span class="math inline">\(a\)</span> modulo <span class="math inline">\(n\)</span></strong> to be the smallest positive integer <span class="math inline">\(k\)</span> such that <span class="math inline">\(a^k \equiv 1 \pmod n\)</span>, when such a number exists.</p>
|
||||
<p>We denote the order of <span class="math inline">\(a\)</span> modulo <span class="math inline">\(n\)</span> as <span class="math inline">\(\text{ord}_n(a)\)</span>.</p>
|
||||
</div>
|
||||
<p>Something you might notice from our above table is that the cycle length for the remainders modulo 7 always divides 6. Here is another table, this time for modulo 17.</p>
|
||||
<table>
|
||||
<thead>
|
||||
<tr class="header">
|
||||
<th><span class="math inline">\(a\)</span></th>
|
||||
<th>Cycle length</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="odd">
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>2</td>
|
||||
<td>8</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>3</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>4</td>
|
||||
<td>4</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>5</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>6</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>7</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>8</td>
|
||||
<td>8</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>9</td>
|
||||
<td>8</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>10</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>11</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>12</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>13</td>
|
||||
<td>4</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>14</td>
|
||||
<td>16</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td>15</td>
|
||||
<td>8</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td>16</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>A similar pattern emerges: the cycle length for these bases always divides 16, which is one less than 17. And again, for each base <span class="math inline">\(a\)</span> other than 0, the cycle length corresponding to <span class="math inline">\(a\)</span> is the least positive integer <span class="math inline">\(k\)</span> such that <span class="math inline">\(a^k \equiv 1 \pmod{17}\)</span>.</p>
|
||||
<p>Here is one more interesting fact about cycle length: because it is a number <span class="math inline">\(k\)</span> such that <span class="math inline">\(a^k \equiv 1 \pmod{17}\)</span>, <em>any</em> multiple <span class="math inline">\(n\)</span> of <span class="math inline">\(k\)</span> also satisfies <span class="math inline">\(a^n \equiv 1 \pmod{17}\)</span>. For example, <span class="math inline">\(13^4 \equiv 1 \pmod{17}\)</span>, and so <span class="math inline">\(13^{40} \equiv (13^4)^{10} \equiv 1^{10} \equiv 1 \pmod{17}\)</span>.</p>
|
||||
<p>Combining these two observations allows us to conclude that, at least for 17, <em>every</em> base <span class="math inline">\(a\)</span> other than 0 satisfies <span class="math inline">\(a^{16} \pmod{17}\)</span>. It is a remarkable fact that this turns out to generalize to every prime number. Proving this theorem is beyond the scope of this course, but we’ll state it formally here to let you marvel at it for a moment.</p>
|
||||
<div class="theorem">
|
||||
<p>(<em>Fermat’s Little Theorem</em>) Let <span class="math inline">\(p, a \in \Z\)</span> and assume <span class="math inline">\(p\)</span> is prime and that <span class="math inline">\(p \nmid a\)</span>. Then <span class="math inline">\(a^{p - 1} \equiv 1 \pmod p\)</span>.</p>
|
||||
</div>
|
||||
<!-- <div translation>
|
||||
$\forall p, a \in \Z,~ \mathit{Prime}(p) \land p \nmid a \Rightarrow a^{p - 1} \equiv 1 \pmod p$.
|
||||
</div>
|
||||
|
||||
<div discussion>
|
||||
This proof is one of the most novel that we'll see in this course.
|
||||
It is based not on a calculation or manipulation of inequalities, but rather a very clever construction of a function with particularly useful properties.
|
||||
We wouldn't expect you to come up with this proof idea yourselves (we certainly didn't when we first learned this!), but you should be able to read through the proof below and understand it.
|
||||
</div>
|
||||
|
||||
<div proof>
|
||||
Let $p, a \in \Z$ and assume $p$ is prime and that $p \nmid a$.
|
||||
We'll prove that $a^{p - 1} \equiv 1 \pmod p$.
|
||||
|
||||
First, we define the following set: let $S = \{x \mid x \in \Z \land 1 \leq x < p \}$.
|
||||
We define the function $f: S \to S$ as $f(x) = ax \% n$, where $\%$ denotes the remainder operation.
|
||||
We leave it as an exercise to prove that $f$ is well-defined, i.e., for all $x \in S$, it's true that $f(x) \in S$ as well.
|
||||
Note that from ths definition, we have $f(x) \equiv ax \pmod p$ for all $x \in S$.
|
||||
Next, we'll prove the following key property of this function $f$.
|
||||
|
||||
**Proving that $f$ is a bijection.**
|
||||
Since $S$ is finite, it is enough to prove that $f$ is one-to-one.
|
||||
Let $x, y \in S$ and assume $f(x) = f(y)$.
|
||||
Then by the definition of $f$, we know that $ax \equiv ay \pmod p$.
|
||||
Since $p \nmid a$, we know that $\gcd(p, a) = 1$, and so by the modular cancellation theorem (**TODO**:check name/reference), we know that $x \equiv y \pmod p$.
|
||||
Since $x$ and $y$ are both in the range 1 to $p - 1$, this means $x = y$.
|
||||
So $f$ is one-to-one.
|
||||
|
||||
Now here is the key idea: consider the product
|
||||
\begin{align*}
|
||||
\prod_{x \in S} f(x) &\equiv \prod_{x \in S} ax \pmod p \tag{since $f(x) \equiv ax \pmod p$} \\
|
||||
\prod_{x \in S} f(x) &\equiv a^{|S|} \prod_{x \in S} x \pmod p
|
||||
\end{align*}
|
||||
But since $f$ is a bijection on $S$, the products $\prod_{x \in S} f(x)$ and $\prod_{x \in S} x$ are equal: they're both expressions multiplying all elements of $S$ modulo $p$!
|
||||
Moreover, since all elements of $S$ have a gcd of $1$ with $p$, we can apply the cancellation law to both sides of the equivalence to obtain
|
||||
$$1 \equiv a^{|S|} \pmod p.$$
|
||||
Finally, we know that $|S| = p - 1$, and so $a^{p-1} \equiv 1 \pmod p$.
|
||||
</div> -->
|
||||
<h2 id="eulers-theorem">Euler’s Theorem</h2>
|
||||
<p>Fermat’s Little Theorem is quite beautiful in its own right, but is limited in scope to prime numbers. It turns out that the key to generalizing this theorem lies with our very last definition in this chapter.</p>
|
||||
<!-- It turns out that we can generalize it to any positive modulus by studying our above proof a bit more closely.
|
||||
Why did we need that $p$ is prime?
|
||||
In two places: so that we could use the assumption $p \nmid a$ to apply the cancellation theorem to $ax \equiv ay \pmod p$,
|
||||
and later to apply the cancellation theorem to
|
||||
$\prod_{x \in S} f(x) \equiv a^{|S|} \prod_{x \in S} x \pmod p$.
|
||||
The cancellation theorem doesn't just apply to prime numbers, though; it applies to any pair of numbers $a$ and $n$ such that $\gcd(a, n) = 1$.
|
||||
This is the key to generalizing Fermat's Little Theorem. -->
|
||||
<div class="definition">
|
||||
<p>We define the function <span class="math inline">\(\varphi : \Z^+ \to \N\)</span>, called the <strong>Euler totient function</strong> (or <strong>Euler phi function</strong>), as follows:</p>
|
||||
<p><span class="math display">\[\varphi(n) = \big| \big\{ a \mid a \in \{1, \dots, n - 1\},~ \text{and $\gcd(a, n) = 1$} \big\} \big|.\]</span></p>
|
||||
</div>
|
||||
<p>Here are some examples of the Euler totient function:</p>
|
||||
<ul>
|
||||
<li><span class="math inline">\(\varphi(5) = 4\)</span>, since <span class="math inline">\(\{1, 2, 3, 4\}\)</span> are all coprime to 5.</li>
|
||||
<li><span class="math inline">\(\varphi(6) = 2\)</span>, since only <span class="math inline">\(\{1, 5\}\)</span> are coprime to 6.</li>
|
||||
<li>In general, for any prime number <span class="math inline">\(p\)</span>, <span class="math inline">\(\varphi(p) = p - 1\)</span>, since all the numbers <span class="math inline">\(\{1, 2, \dots, p - 1\}\)</span> are coprime to <span class="math inline">\(p\)</span>.<label for="sn-3" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-3" class="margin-toggle"/><span class="sidenote"> Exercise: prove this using the definition of prime!</span></li>
|
||||
<li><span class="math inline">\(\varphi(15) = 8\)</span>, since the numbers <span class="math inline">\(\{1, 2, 4, 7, 8, 11, 13, 14\}\)</span> are all coprime to 15. Note that the ``removed’’ numbers are all multiples of 3 or 5, the prime factors of 15.</li>
|
||||
<li>In general, for any two distinct primes <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span>, <span class="math inline">\(\varphi(pq) = (p - 1)(q - 1)\)</span>, although this is certainly not obvious, and requires a proof!</li>
|
||||
</ul>
|
||||
<p>With the Euler totient function in hand, we can now state the generalization of Fermat’s Little Theorem, which is something we’ll use in the next chapter.</p>
|
||||
<div id="theorem:eulers-theorem" class="theorem">
|
||||
<p>(<em>Euler’s Theorem</em>). For all <span class="math inline">\(a \in \Z\)</span> and <span class="math inline">\(n \in \Z^+\)</span>, if <span class="math inline">\(\gcd(a, n) = 1\)</span> then <span class="math inline">\(a^{\varphi(n)} \equiv 1 \pmod n\)</span>.</p>
|
||||
<!-- <div class="proof">
|
||||
Let $n \in \Z^+$ and let $a \in \Z$, and assume $\gcd(a, n) = 1$.
|
||||
|
||||
Let $S = \{x \mid x \in \Z \land 1 \leq x < n \land \gcd(x, n) = 1 \}$.
|
||||
We define the function $f: S \to S$ as $f(x) = ax \% n$, where $\%$ denotes the remainder operation.
|
||||
We leave it as an exercise to prove that $f$ is well-defined, i.e., for all $x \in S$, it's true that $f(x) \in S$ as well.
|
||||
Note that from ths definition, we have $f(x) \equiv ax \pmod n$ for all $x \in S$.
|
||||
|
||||
First, we prove that $f$ is a bijection.
|
||||
Since $S$ is finite, it is enough to prove that $f$ is one-to-one.
|
||||
Let $x, y \in S$ and assume $f(x) = f(y)$.
|
||||
Then by the definition of $f$, we know that $ax \equiv ay \pmod n$.
|
||||
By the cancellation theorem, since $\gcd(a, n) = 1$, we know that $x \equiv y \pmod n$.
|
||||
Since $x$ and $y$ are both in the range 1 to $n - 1$, this means $x = y$.
|
||||
So $f$ is one-to-one.
|
||||
|
||||
Now here is the key idea: consider the product
|
||||
\begin{align*}
|
||||
\prod_{x \in S} f(x) &\equiv \prod_{x \in S} ax \pmod n \tag{since $f(x) \equiv ax \pmod n$} \\
|
||||
\prod_{x \in S} f(x) &\equiv a^{|S|} \prod_{x \in S} x \pmod n
|
||||
\end{align*}
|
||||
But since $f$ is a bijection on $S$, the products $\prod_{x \in S} f(x)$ and $\prod_{x \in S} x$ are equal: they're both expressions multiplying all elements of $S$!
|
||||
Moreover, since all elements of $S$ have a gcd of $1$ with $n$, we can apply the cancellation law to both sides of the equivalence to obtain
|
||||
$$1 \equiv a^{|S|} \pmod n.$$
|
||||
Finally, by the definition of the Euler totient function, we know that $|S| = \varphi(n)$, and so $a^{\varphi(n)} \equiv 1 \pmod n$.
|
||||
</div> -->
|
||||
</div>
|
||||
</section>
|
||||
<footer>
|
||||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user