ebook img

From Mathematics to Generic Programming PDF

311 Pages·2014·2.418 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview From Mathematics to Generic Programming

From Mathematics to Generic Programming This page intentionally left blank From Mathematics to Generic Programming Alexander A. Stepanov Daniel E. Rose UpperSaddleRiver,NJ(cid:15)Boston(cid:15)Indianapolis(cid:15)SanFrancisco NewYork(cid:15)Toronto(cid:15)Montreal(cid:15)London(cid:15)Munich(cid:15) Paris(cid:15)Madrid Capetown(cid:15)Sydney(cid:15)Tokyo(cid:15)Singapore(cid:15)MexicoCity Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimed astrademarks.Wherethosedesignationsappearinthisbook,andthepublisherwasawareofatrade- markclaim,thedesignationshavebeenprintedwithinitialcapitallettersorinallcapitals. eauthorsandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressed orimpliedwarrantyofanykindandassumenoresponsibilityforerrorsoromissions.Noliabilityis assumedforincidentalorconsequentialdamagesinconnectionwithorarisingoutoftheuseofthe informationorprogramscontainedherein. Forinformationaboutbuyingthistitleinbulkquantities,orforspecialsalesopportunities(which mayincludeelectronicversions;customcoverdesigns;andcontentparticulartoyourbusiness,train- inggoals,marketingfocus,orbrandinginterests),pleasecontactourcorporatesalesdepartmentat [email protected](800)382-3419. Forgovernmentsalesinquiries,[email protected]. ForquestionsaboutsalesoutsidetheUnitedStates,[email protected]. VisitusontheWeb:informit.com/aw LibraryofCongressCataloging-in-PublicationData Stepanov,AlexanderA. Frommathematicstogenericprogramming/AlexanderA.Stepanov,DanielE.Rose. pages cm Includesbibliographicalreferencesandindex. ISBN978-0-321-94204-3(pbk.:alk.paper) 1. Genericprogramming(Computerscience)—Mathematics. 2. Computeralgorithms. I.Rose, DanielE.II.Title. QA76.6245.S742015 005.1'1—dc23 2014034539 Copyright©2015PearsonEducation,Inc. Allrightsreserved.PrintedintheUnitedStatesofAmerica.ispublicationisprotectedbycopy- right,andpermissionmustbeobtainedfromthepublisherpriortoanyprohibitedreproduction, storageinaretrievalsystem,ortransmissioninanyformorbyanymeans,electronic,mechanical, photocopying,recording,orlikewise. Toobtainpermissiontousematerialfromthiswork,please submitawrittenrequesttoPearsonEducation,Inc.,PermissionsDepartment,OneLakeStreet,Up- perSaddleRiver,NewJersey07458,oryoumayfaxyourrequestto(201)236-3290. Photocreditsarelistedonpage293. ISBN-13:978-0-321-94204-3 ISBN-10:0-321-94204-3 TextprintedintheUnitedStatesonrecycledpaperatRRDonnelleyinCrawfordsville,Indiana. irdprinting,August2015 Contents Acknowledgments ix AbouttheAuthors xi Authors’Note xiii 1 WhatThisBookIsAbout 1 1.1 ProgrammingandMathematics 2 1.2 AHistoricalPerspective 2 1.3 Prerequisites 3 1.4 Roadmap 4 2 TheFirstAlgorithm 7 2.1 EgyptianMultiplication 8 2.2 ImprovingtheAlgorithm 11 2.3 ThoughtsontheChapter 15 3 AncientGreekNumberTheory 17 3.1 GeometricPropertiesofIntegers 17 3.2 SiftingPrimes 20 3.3 ImplementingandOptimizingtheCode 23 3.4 PerfectNumbers 28 3.5 ThePythagoreanProgram 32 3.6 AFatalFlawintheProgram 34 3.7 ThoughtsontheChapter 38 4 Euclid’sAlgorithm 41 4.1 AthensandAlexandria 41 4.2 Euclid’sGreatestCommonMeasureAlgorithm 45 4.3 AMillenniumwithoutMathematics 50 4.4 TheStrangeHistoryofZero 51 4.5 RemainderandQuotientAlgorithms 53 4.6 SharingtheCode 57 4.7 ValidatingtheAlgorithm 59 4.8 ThoughtsontheChapter 61 v vi Contents 5 TheEmergenceofModernNumberTheory 63 5.1 MersennePrimesandFermatPrimes 63 5.2 Fermat’sLittleTheorem 69 5.3 Cancellation 72 5.4 ProvingFermat’sLittleTheorem 77 5.5 Euler’sTheorem 79 5.6 ApplyingModularArithmetic 83 5.7 ThoughtsontheChapter 84 6 AbstractioninMathematics 85 6.1 Groups 85 6.2 MonoidsandSemigroups 89 6.3 SomeTheoremsaboutGroups 92 6.4 SubgroupsandCyclicGroups 95 6.5 Lagrange’sTheorem 97 6.6 TheoriesandModels 102 6.7 ExamplesofCategoricalandNon-categoricalTheories 104 6.8 ThoughtsontheChapter 107 7 DerivingaGenericAlgorithm 111 7.1 UntanglingAlgorithmRequirements 111 7.2 RequirementsonA 113 7.3 RequirementsonN 116 7.4 NewRequirements 118 7.5 TurningMultiplyintoPower 119 7.6 GeneralizingtheOperation 121 7.7 ComputingFibonacciNumbers 124 7.8 ThoughtsontheChapter 127 8 MoreAlgebraicStructures 129 8.1 Stevin,Polynomials,andGCD 129 8.2 GöttingenandGermanMathematics 135 8.3 NoetherandtheBirthofAbstractAlgebra 140 8.4 Rings 142 8.5 MatrixMultiplicationandSemirings 145 8.6 Application: SocialNetworksandShortestPaths 147 8.7 EuclideanDomains 150 8.8 FieldsandOtherAlgebraicStructures 151 8.9 ThoughtsontheChapter 152 9 OrganizingMathematicalKnowledge 155 9.1 Proofs 155 9.2 TheFirstTheorem 159 Contents vii 9.3 EuclidandtheAxiomaticMethod 161 9.4 AlternativestoEuclideanGeometry 164 9.5 Hilbert’sFormalistApproach 167 9.6 PeanoandHisAxioms 169 9.7 BuildingArithmetic 173 9.8 ThoughtsontheChapter 176 10 FundamentalProgrammingConcepts 177 10.1 AristotleandAbstraction 177 10.2 ValuesandTypes 180 10.3 Concepts 181 10.4 Iterators 184 10.5 IteratorCategories,Operations,andTraits 185 10.6 Ranges 188 10.7 LinearSearch 190 10.8 BinarySearch 191 10.9 ThoughtsontheChapter 196 11 PermutationAlgorithms 197 11.1 PermutationsandTranspositions 197 11.2 SwappingRanges 201 11.3 Rotation 204 11.4 UsingCycles 207 11.5 Reverse 212 11.6 SpaceComplexity 215 11.7 Memory-AdaptiveAlgorithms 216 11.8 ThoughtsontheChapter 217 12 ExtensionsofGCD 219 12.1 HardwareConstraintsandaMoreEfficientAlgorithm 219 12.2 GeneralizingStein’sAlgorithm 222 12.3 Bézout’sIdentity 225 12.4 ExtendedGCD 229 12.5 ApplicationsofGCD 234 12.6 ThoughtsontheChapter 234 13 AReal-WorldApplication 237 13.1 Cryptology 237 13.2 PrimalityTesting 240 13.3 TheMiller-RabinTest 243 13.4 TheRSAAlgorithm: HowandWhyItWorks 245 13.5 ThoughtsontheChapter 248 viii Contents 14 Conclusions 249 FurtherReading 251 A Notation 257 B CommonProofTechniques 261 B.1 ProofbyContradiction 261 B.2 ProofbyInduction 262 B.3 ThePigeonholePrinciple 263 C C++forNon-C++Programmers 265 C.1 TemplateFunctions 265 C.2 Concepts 266 C.3 DeclarationSyntaxandTypedConstants 267 C.4 FunctionObjects 268 C.5 Preconditions,Postconditions,andAssertions 269 C.6 STLAlgorithmsandDataStructures 269 C.7 IteratorsandRanges 270 C.8 TypeAliasesandTypeFunctionswithusinginC++11 272 C.9 InitializerListsinC++11 272 C.10LambdaFunctionsinC++11 272 C.11ANoteaboutinline 273 Bibliography 275 Index 281 Acknowledgments We would like to thank all the people who contributed to making this book a reality. OurmanagementatA9.comactivelysupportedthisprojectfromthebe- ginning.BillStasiorinitiatedthecreationofthecoursethisbookisbasedon,and selectedthetopicfromamongseveraloptionsweoffered. BrianPinkertonnot onlyattendedthecourse,butalsostronglyencouragedourideaofturningthe materialintoabook. WealsowouldliketothankMatMarcus,whocollaborated withAlexonasimilarcourseatAdobein2004–2005. e other members of the Fundamental Data Structures and Algorithms forSearchteamplayedimportantrolesthroughouttheprocess. AnilGangolli helpedshapethecontentofthecourse, RyanErnstprovidedmuchofthepro- gramminginfrastructure,andParamjitOberoigaveinvaluablefeedbackduring thewritingstage. Wehaveenjoyedworkingwithallofthemandaregratefulfor theirinput. Wearegratefultooureditors, PeterGordonandGregDoench, andtothe teamofexpertsassembledbyAddison-Wesley,includingmanagingeditorJohn Fuller, production editor Mary Kesel Wilson, copyeditor Jill Hobbs, and com- positor/LaTeXexpertLoriHughesforalltheirworkinturningourroughmanu- scriptintoapolishedbook. Finally,we’dliketothankthemanyfriends,familymembers,andcolleagues whoreadearlierdrasofthebookand/orgaveuscomments,corrections,sug- gestions,advice,orotherhelp: GašperAžman,JohnBanning,CynthiaDwork, Hernan Epelman, Ryan Ernst, Anil Gangolli, Susan Gruber, Jon Kalb, Robert Lehr, Dmitry Leshchiner, Tom London, Mark Manasse, Paul McJones, Nico- lasNicolov,GorNishanov,ParamjitOberoi,SeanParent,FernandoPelliccioni, JohnReiser,RobertRose,StefanVargyas,andAdamYoung. ebookismuch betterasaresultoftheircontributions. Severalcarefulreaderssentcorrectionstoerrorsthatoccurredinthefirstand secondprintingsofthisbook,orinthedigitaleditions. ankstothefollowing peopleforreportingtheseerrors: AbutalibAghayev,VladimirBurenkov,Greg ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.