Three Algorithmic Journeys * * * DRAFT 0.3 *
Total Page:16
File Type:pdf, Size:1020Kb
Three Algorithmic Journeys * * * DRAFT 0.3 * * * Alexander A. Stepanov Daniel E. Rose January 4, 2013 c 2012, 2013 by Alexander A. Stepanov and Daniel E. Rose i ii January 4, 2013 *** DRAFT 0.3 *** Contents Authors’ Note vii Prologue 1 Journey One: Legacy of Ahmes 7 1 Egyptian Multiplication 7 1.1 Proofs . .7 1.2 Mathematical Traditions Around the World . 12 1.3 Thales and Egyptian Mathematics . 12 1.4 Ahmes’ Multiplication Algorithm . 15 1.5 Improving the Algorithm . 18 1.6 Rewriting Code . 21 2 From Multiplication to Power 23 2.1 Untangling the Requirements . 23 2.2 Requirements on A ................................. 24 2.3 Requirements on N ................................. 27 2.4 Semigroups, Monoids, and Groups . 29 2.5 Turning Multiply Into Power . 32 2.6 Generalizing the Operation . 33 2.7 Reduction . 36 2.8 Fibonacci Numbers . 37 2.9 Generalizing Matrix Multiplication . 39 3 Greek Origins of Number Theory 43 3.1 Pythagoras and Some Geometric Properties of Integers . 43 3.2 Sifting Primes . 46 3.3 Perfect Numbers . 52 iii iv CONTENTS 3.4 A Millennium Without Mathematics . 56 4 The Emergence of Modern Number Theory 59 4.1 Mersenne Primes and Fermat Primes . 59 4.2 The Little Fermat Theorem . 62 4.3 Rethinking Our Results Using Modular Arithmetic . 70 4.4 Euler’s Theorem . 72 5 Public-Key Cryptography 77 5.1 Primality Testing . 77 5.2 Cryptology . 81 5.3 The RSA Algorithm: How and Why It Works . 83 5.4 Lessons of the Journey . 86 Journey Two: Heirs of Pythagoras 89 6 Euclid and the Greatest Common Measure 89 6.1 The Original Pythagorean Program . 89 6.2 A Fatal Flaw in the Program . 91 6.3 Athens and Alexandria . 95 6.4 Greatest Common Measure, Revisited . 96 6.5 The Strange History of Zero . 101 6.6 Remainder, Quotient, and GCD . 103 7 From Line Segments to Concepts 109 7.1 Polynomials and GCD . 109 7.2 Göttingen and German Mathematics . 114 7.3 Noether and the Birth of Abstract Algebra . 119 7.4 Groups . 120 7.5 Subgroups, Cyclic Groups, and Cosets . 123 7.6 Lagrange’s Theorem . 125 8 More Abstractions and the Domain of GCD 129 8.1 Rings . 129 8.2 Euclidean Domains . 132 8.3 Fields . 133 8.4 A Faster GCD . 134 8.5 Generalizing Stein’s Algorithm . 136 8.6 Lessons of Stein . 140 January 4, 2013 *** DRAFT 0.3 *** CONTENTS v 9 Extensions of GCD 143 9.1 Bézout’s Identity . 143 9.2 A Constructive Proof . 146 9.3 Applications of GCD . 150 9.4 Permutations and Transpositions . 151 10 Rotate and Reverse 155 10.1 Swapping Ranges . 155 10.2 Rotation . 158 10.3 Using Cycles . 162 10.4 Reverse . 164 10.5 Space Complexity . 168 10.6 Memory-Adaptive Algorithms . 169 10.7 Lessons of the Journey . 170 Journey Three: Successors of Peano 173 11 Numbers from the Ground Up 173 11.1 Euclid and the Axiomatic Method . 173 11.2 The Overthrow of Euclidean Geometry . 175 11.3 Hilbert’s Approach . 178 11.4 Peano and His Axioms . 179 11.5 Building Arithmetic . 182 12 Sets and Infinities 185 12.1 Tackling the Infinite . 185 12.2 Paradoxes of the Infinite . 188 12.3 Levels of Infinity . 189 12.4 Set Theory . 191 12.5 Diagonalization . 195 12.6 The Continuum Hypothesis . 198 12.7 Axiomatizing Set Theory . 199 13 From Axioms to Computability 203 13.1 Hilbert’s Program . 203 13.2 The Program Collapses . 204 13.3 Church, Turing, and the Origins of Computer Science . 206 13.4 Diagonalization Revisited . 207 *** DRAFT 0.3 *** January 4, 2013 vi CONTENTS 14 Theories and Concepts 213 14.1 Aristotle’s Organization of Knowledge . 213 14.2 Theories and Models . 215 14.3 Values and Types . 218 14.4 Concepts . 218 14.5 Three Fundamental Programming Tasks . 221 15 Iterators and Search 223 15.1 Iterators . 223 15.2 Iterator Categories, Operations, and Traits . 224 15.3 Ranges Revisited . 227 15.4 Linear Search . 228 15.5 Binary Search . 230 15.6 Lessons of the Journey . 234 Epilogue 235 Further Reading 237 Appendix: C++ Language Features 241 Bibliography 243 Index 245 January 4, 2013 *** DRAFT 0.3 *** Authors’ Note The book you are about to read is based on notes from a course taught by Alex Stepanov at A9.com during 2012. However, it is not a transcript of the lectures; rather, it is a re- imagining of the course material in prose. In some cases topics have been reorganized and details have been added or removed to better suit the needs of the medium and to make the material more accessible to a less mathematically advanced reader. While Alex comes from a mathematical background, I do not. I’ve tried to learn from my own struggles to understand some of the material, and to use this experience to identify ideas that require additional explanation. If in some cases we describe something in a slightly different way that a mathematician would, or using slightly different terminology, the fault is mine. Those who have heard about the course or seen the lectures might notice that the course was originally called Four Algorithmic Journeys, while this book contains only three. As the course evolved, Alex realized that the three journeys presented here form a coherent whole: each journey has an associated branch of mathematics (Number The- ory, Abstract Algebra, and Logic/Set Theory, respectively), which the originally imagined fourth journey did not have. [The current work is only a draft. You will find many places where there are parenthet- ical notes like “More explanation here” or “Biography goes here.” These notes, which are placeholders for changes and editions in the final version, are shown in square brackets.] — D.E.R. This book is about journeys — the algorithmic journeys of the title, but also my own journey through life, encountering towering figures of mathematics through their work; along the way, I met wonderful people like Pythagoras, Plato, Euclid, and Archimedes, Fermat, Descartes, Euler, Gauss, and Cauchy, and now I’m sharing what they told me with you. I will try to present their message within our setting of modern programming. Although I have always enjoyed mathematics and was fortunate enough to study with some great mathematicians early in my life, the ideas really came alive when I started to learn the history of the field and some of its heroes. So that’s the approach we’re going to take here. I have always found that learning the connections and the context of an idea vii viii AUTHORS’ NOTE helps me understand it better. Hopefully some of you will find this helpful as well. Many have been conditioned to believe that mathematics is boring, but I hope to show you that it isn’t. If at the end of the book, some readers decide to buy a copy of Euclid’s Elements, I’ll know that I’ve been successful. — A.A.S. January 4, 2013 *** DRAFT 0.3 *** Prologue God always does geometry. – Plato But thou hast arranged all things by measure and number and weight. – Wisdom of Solomon It is impossible to know about this world without knowing mathematics. – Roger Bacon This book might be described as “math that programmers should know.” At first glance it might seem that programmers don’t need to know much about math; it’s cer- tainly possible to be a programmer without it. In fact, you might argue that very few jobs actually require knowledge of mathematics, so there’s not much point.