1 Appetizer: Integer Arithmetics F R E Anappetizerissupposedtostimulatetheappetiteatthebeginningofameal.Thisis exactlythepurposeofthischapter.Wewanttostimulateyourinterestinalgorithmic1 techniquesbyshowinEgyouasurprisingresult.Theschoolmethodformultiplyingin- tegersisnotthebestmultiplicationalgorithm;therearemuchfasterwaystomultiply large integers, i.e.,integerswith thousandsorevenmillionsof digits,andwe shall teachyouoneofthem. Arithmeticon longintegersis neededin areassuch ascryptography,geometric computing,andcomputeralgebrCaandsoanimprovedmultiplicationalgorithmisnot justanintellectualgembutalsousefulforapplications.Ontheway,weshalllearn basic analysis and basic algorithm engineering techniques in a simple setting. We shallalsoseetheinterplayoftheoryandexperiment. We assume that integers are represented as digit strings. In the base B number system, where B is an integer larger thaOn one, there are digits 0, 1, to B−1 and a digitstringa a ...a a representsthenumber(cid:229) aBi.Themostimportant n−1 n−2 1 0 0≤i<n i systemswithasmallvalueofBarebase2,withdigits0and1,base10,withdigits0 to9,andbase16,withdigits0to15(frequentlywrittenas0to9,A,B,C,D,E,and F).Largerbases,suchas28,216,232,and264,arealsouseful.Forexample, P “10101” inbase2represents 1·24+0·23+1·22+0·21+1·20= 21, “924” inbase10represents 9·102+2·101+4·100= 924. We assume that we have two primitive operationsaYt our disposal: the addition ofthreedigitswithatwo-digitresult(thisissometimescalledafulladder),andthe 1TheSovietstamponthispageshowsMuhammadibnMusaal-Khwarizmi(bornapproxi- mately780;diedbetween835and850),Persianmathematicianandastronomerfromthe Khorasanprovinceofpresent-dayUzbekistan. Theword“algorithm”isderivedfromhis name. 2 1 Appetizer:IntegerArithmetics multiplication of two digits with a two-digit result.2 For example, in base 10, we have 3 5 and 6·7=42. 5 F 13 Weshallmeasuretheefficiencyofouralgorithmsbythenumberofprimitiveopera- tionsexecuted. Wecanartificiallyturnanyn-digitintegerintoanm-digitintegerforanym≥nby R addingadditionalleadingzeros.Concretely,“425”and“000425”representthesame integer.We shall use a andb for the two operandsof an additionor multiplication andassumethroughoutthissectionthataandbaren-digitintegers.Theassumption thatbothoperandshavethesamelengthsimplifiesthepresentationwithoutchanging thekeymessageofthechapter.Weshallcomebacktothisremarkattheendofthe E chapter.Werefertothedigitsofaasa toa ,witha beingthemostsignificant n−1 0 n−1 digit (also called leading digit) and a being the least significant digit, and write 0 a=(a ...a ). The leading digit may be zero. Similarly, we use b to b to n−1 0 n−1 0 denotethedigitsofb,andwriteb=(b ...b ). n−1 0 E 1.1 Addition We all know how to add two integers a=(a ...a ) and b=(b ...b ). We n−1 0 n−1 0 simply write one under the other with the least significant digits aligned, and sum theintegersdigitwise,carryingaCsingledigitfromonepositiontothenext.Thisdigit iscalledacarry.Theresultwillbeann+1-digitintegers=(s ...s ).Graphically, n 0 a ...a a firstoperand n−1 1 0 b ...b b secondoperand n−1 1 0 cn cn−1 ... c1 0 Ocarries s s ... s s sum n n−1 1 0 wherec toc isthesequenceofcarriesands=(s ...s )isthesum.Wehavec =0, n 0 n 0 0 c ·B+s =a +b +c for0≤i<nands =c .Asaprogram,thisiswrittenas i+1 i i i i n n c=0 :Digit // Variableforthecarrydigit P fori:=0ton−1do adda,b,andctoforms andanewcarryc i i i s :=c n Weneedoneprimitiveoperationforeachposition,andhenceatotalofnprimi- tiveoperations. Y Theorem1.1.Theadditionoftwon-digitintegersrequiresexactlynprimitiveoper- ations.Theresultisann+1-digitinteger. 2Observethatthesumofthreedigitsisatmost3(B−1)andtheproductoftwodigitsisat most(B−1)2,andthatbothexpressionsareboundedby(B−1)·B1+(B−1)·B0=B2−1, thelargestintegerthatcanbewrittenwithtwodigits. 1.2 Multiplication:TheSchoolMethod 3 1.2 Multiplication:The School Method Weallknowhowtomultiplytwointegers.Inthissection,weshallreviewthe“school method”. In a later section, we shall get to know a method which is significantly fasterforlargeintegers. FWeshallproceedslowly.Wefirstreviewhowtomultiplyann-digitintegeraby aone-digitintegerb .Weuseb fortheone-digitinteger,sincethisishowweneed j j itbelow.Foranydigita ofa,weformthe producta ·b .Theresultisa two-digit i i j integer(cd),i.e., i i Ra ·b =c ·B+d . i j i i Weformtwointegers,c=(c ...c 0)andd=(d ...d ),fromthec’sandd’s, n−1 0 n−1 0 respectively.Sincethec’sarethehigher-orderdigitsintheproducts,weaddazero digitattheend.Weaddcanddtoobtaintheproductp =a·b .Graphically, j j Ec c ... c c ... c 0 n−1 n−2 i i−1 0 (an−1...ai...a0)·bj −→ dn−1 ... di+1 di ... d1 d0 sumofcandd Letusdeterminethenumberofprimitiveoperations.Foreachi,weneedoneprim- E itiveoperationtoformtheproducta ·b ,foratotalofnprimitiveoperations.Then i j weaddtwon+1-digitnumbers.Thisrequiresn+1primitiveoperations.Sothetotal numberofprimitiveoperationsis2n+1. Lemma1.2.We canmultiplyann-digitnumberbyaone-digitnumberwith 2n+1 primitiveoperations.Theresultisann+1-digitnumber. Whenyoumultiplyann-digCitnumberbyaone-digitnumber,youwillprobably proceedslightlydifferently.Youcombine3thegenerationoftheproductsa ·b with i j the summationof c and d into a single phase, i.e., you create the digitsof c and d when they are needed in the final addition. We have chosen to generate them in a separatephasebecausethissimplifiesthedescriptionofthealgorithm. O Exercise1.1.Give a program for the multiplication of a and b that operates in a j singlephase. Wecannowturntothemultiplicationoftwon-digitintegers.Theschoolmethod forintegermultiplicationworksasfollows:wefirstformpartialproductsp bymul- j tiplyingabythe j-thdigitb ofb,andthensumtPhesuitablyalignedproductsp ·Bj j j toobtaintheproductofaandb.Graphically, p p ... p p p 0,n 0,n−1 0,2 0,1 0,0 p p p ... p p 1,n 1,n−1 1,n−2 1,1 1,0 Y p p p p ... p 2,n 2,n−1 2,n−2 2,n−3 2,0 ... p ... p p p p n−1,n n−1,3 n−1,2 n−1,1 n−1,0 sumofthenpartialproducts 3Intheliteratureoncompilerconstructionandperformanceoptimization,thistransforma- tionisknownasloopfusion. 4 1 Appetizer:IntegerArithmetics Thedescriptioninpseudocodeismorecompact.Weinitializetheproduct ptozero andthenaddtoitthepartialproductsa·b ·Bj onebyone: j p=0 : for j:=N0ton−1do p:=p+a·b ·Bj j F Letusanalyzethenumberofprimitiveoperationsrequiredbytheschoolmethod. Each partial product p requires 2n+1 primitive operations, and hence all partial j products together require 2n2+n primitive operations. The product a·b is a 2n- digit number, and hence all summations p+a·b ·Bj are summations of 2n-digit j integRers.Eachsuchadditionrequiresatmost2nprimitiveoperations,andhenceall additionstogetherrequireatmost2n2 primitiveoperations.Thus,weneednomore than4n2+nprimitiveoperationsintotal. Asimpleobservationallowsustoimprovethisbound.Thenumbera·b ·Bj has j n+1+ j digits, the last j of which are zero.We can thereforestart the additionin the j+1-thpEosition.Also,whenweadda·b ·Bj to p,wehave p=a·(b ···b ), j j−1 0 i.e., phasn+jdigits.Thus,theadditionof panda·b ·Bj amountstotheaddition j oftwon+1-digitnumbersandrequiresonlyn+1primitiveoperations.Therefore, alladditionstogetherrequireonlyn2+nprimitiveoperations.Wehavethusshown thefollowingresult.E Theorem1.3.Theschoolmethodmultipliestwon-digitintegerswith3n2+2nprim- itiveoperations. We have now analyzed the numbers of primitive operations required by the school methods for integer addition and integer multiplication. The number M of n primitive operations for the school method for integer multiplication is 3n2+2n. C Observethat3n2+2n=n2(3+2/n),andhence3n2+2nisessentiallythesameas 3n2forlargen.WesaythatM growsquadratically.Observealsothat n 3n2+2n n2(3+2/n) 3n+2 M /M = = =4· ≈4, n n/2 3(n/2)2+2(n/2) (n/2)2(3+4/n) 3n+4 O i.e.,quadraticgrowthhastheconsequenceofessentiallyquadruplingthenumberof primitiveoperationswhenthesizeoftheinstanceisdoubled. Assumenowthatweactuallyimplementthemultiplicationalgorithminourfa- voriteprogramminglanguage(weshalldosolaterinthechapter),andthentimethe programonourfavoritemachineforvariousn-dPigitintegersaandbandvariousn. Whatshouldweexpect?We wanttoarguethatwe shallsee quadraticgrowth.The reasonis thatprimitive operationsare representative ofthe runningtime ofthe al- gorithm.Considertheadditionoftwon-digitintegersfirst.Whathappenswhenthe programisexecuted?Foreachpositioni,thedigitsaianYdbihavetobemovedtothe processingunit,thesuma +b +chastobeformed,thedigits oftheresultneeds i i i tobestoredinmemory,thecarrycisupdated,theindexiisincremented,andatest forloopexitneedstobeperformed.Thus,foreachi,thesamenumberofmachine cycles is executed.We have counted one primitive operationfor each i, and hence the number of primitiveoperationsis representativeof the number of machine cy- clesexecuted.Ofcourse,thereareadditionaleffects,forexamplepipeliningandthe 1.2 Multiplication:TheSchoolMethod 5 n Tn(sec) Tn/Tn/2 school method 8 0.00000469 100 16 0.0000154 3.28527 32 0.0000567 3.67967 10 F64 0.000222 3.91413 128 0.000860 3.87532 1 256 0.00347 4.03819 c] 512 0.0138 3.98466 e [se 0.1 1R024 0.0547 3.95623 tim 0.01 2048 0.220 4.01923 4096 0.880 4 0.001 8192 3.53 4.01136 16384 14.2 4.01416 0.0001 32768 5E6.7 4.00212 65536 227 4.00635 131072 910 4.00449 24 26 28 210212214216 n E Fig.1.1.Therunningtimeoftheschoolmethodforthemultiplicationofn-digitintegers.The threecolumnsofthetableontheleftgiven,therunningtimeTnoftheC++implementation giveninSect.1.7,andtheratioTn/Tn/2.TheplotontherightshowslogTnversuslogn,andwe seeessentiallyaline.ObservethatifTn=a nb forsomeconstantsa andb ,thenTn/Tn/2=2b andlogTn=b logn+loga ,i.e.,logTndependslinearlyonlognwithslopeb .Inourcase,the slopeistwo.Please,usearulertocheck C complextransportmechanismfordatabetweenmemoryandtheprocessingunit,but theywillhaveasimilareffectforalli,andhencethenumberofprimitiveoperations is also representativeof the runningtime of an actualimplementationon an actual machine.TheargumentextendstomultOiplication,sincemultiplicationofa number by a one-digitnumberis a processsimilar to addition and the second phase of the schoolmethodformultiplicationamountstoaseriesofadditions. Letusconfirmtheaboveargumentbyanexperiment.Figure1.1showsexecution timesofaC++implementationoftheschoolmethod;theprogramcanbefoundin Sect. 1.7. For each n, we performed a large number4 of multiplications of n-digit P random integers and then determined the average running time T ; T is listed in n n the second column. We also show the ratio T /T . Figure 1.1 also shows a plot n n/2 ofthedatapoints5(logn,logT ).Thedataexhibitsapproximatelyquadraticgrowth, n as we can deduce in various ways. The ratio T /T is always close to four, and n n/2 Y the doublelogarithmicplotshowsessentially a line of slope two. The experiments 4TheinternalclockthatmeasuresCPUtimereturnsitstimingsinsomeunits,saymillisec- onds,andhencetheroundingrequiredintroducesanerrorofuptoone-halfofthisunit.It isthereforeimportantthattheexperimenttimedtakesmuchlongerthanthisunit,inorder toreducetheeffectofrounding. 5Throughoutthisbook,weuselogxtodenotethelogarithmtobase2,log x. 2 6 1 Appetizer:IntegerArithmetics arequiteencouraging:ourtheoreticalanalysishaspredictivevalue.Ourtheoretical analysisshowedquadraticgrowthofthenumberofprimitiveoperations,weargued abovethattherunningtimeshouldberelatedtothenumberofprimitiveoperations, andthe actualrunningtime essentially grows quadratically.However,we also see systematicdeviations.Forsmalln,thegrowthfromonerowtothenextislessthanby F afactoroffour,aslinearandconstanttermsintherunningtimestillplayasubstantial role.Forlargern,theratioisveryclosetofour.Forverylargen(toolargetobetimed conveniently),wewouldprobablyseeafactorlargerthanfour,sincetheaccesstime to memory depends on the size of the data. We shall come back to this point in R Sect.2.2. Exercise1.2.Write programs for the addition and multiplication of long integers. Representintegersassequences(arraysorlistsorwhateveryourprogramminglan- guageoffers)ofdecimaldigitsandusethebuilt-inarithmetictoimplementtheprim- itiveoperatioEns.ThenwriteADD,MULTIPLY1,andMULTIPLYfunctionsthatadd integers, multiply an integer by a one-digit number,and multiply integers, respec- tively.UseyourimplementationtoproduceyourownversionofFig.1.1.Experiment withusingalargerbasethanbase10,saybase216. E Exercise1.3.Describeandanalyzetheschoolmethodfordivision. 1.3 ResultChecking Ouralgorithmsforadditionandmultiplicationarequitesimple,andhenceitisfair toassumethatwecanimplemenCtthemcorrectlyintheprogramminglanguageofour choice.However,writingsoftware6 isan error-proneactivity,andhenceweshould alwaysaskourselveswhetherwecanchecktheresultsofacomputation.Formulti- plication,theauthorsweretaughtthefollowingtechniqueinelementaryschool.The method is known as Neunerprobe in German, “casting out nines” in English, and O preuveparneuf inFrench. Add the digits of a. If the sum is a number with more than one digit, sum its digits.Repeatuntilyouarriveataone-digitnumber,calledthechecksumofa.We uses todenotethischecksum.Hereisanexample: a 4528→19→10→P1. Do the same for b and the result c of the computation. This gives the checksums s and s . All checksums are single-digit numbers. Compute s ·s and form its b c a b checksum s. If s differs from s , c is not equal to a·b. This test was described by c Y al-Khwarizmiinhisbookonalgebra. Let us go through a simple example. Let a=429, b=357, and c=154153. Then s =6, s =6, and s =1. Also, s ·s =36 and hences=9. So s 6=s and a b c a b c 6Thebug in thedivision algorithm of thefloating-point unit of the original Pentiumchip became infamous. It was caused by a few missing entries in a lookup table used by the algorithm. 1.4 ARecursiveVersionoftheSchoolMethod 7 hence s is not the product of a and b. Indeed, the correct productis c=153153. c Itschecksumis9,andhencethecorrectproductpassesthetest.Thetestisnotfool- proof,asc=135153alsopassesthetest.However,thetestisquiteusefulanddetects manymistakes. What is the mathematics behind this test? We shall explain a more general F method.Letqbeanypositiveinteger;inthemethoddescribedabove,q=9.Lets a betheremainder,orresidue,intheintegerdivisionofabyq,i.e.,s =a−⌊a/q⌋·q. a Then0≤s <q.Inmathematicalnotation,s =amodq.7 Similarly,s =bmodq a a b and s =cmodq. Finally, s=(s ·s )modq. If c=a·b, then it mustbe the case c a b R thats=s .Thuss6=s provesc6=a·banduncoversamistakeinthemultiplication. c c What do we know if s=s ? We know that q divides the difference of c and a·b. c If this differenceis nonzero,the mistake will be detected by anyq whichdoesnot dividethedifference. Letuscontinuewithourexampleandtakeq=7.Thenamod7=2,bmod7=0 E andhences=(2·0)mod7=0.But135153mod7=4,andwehaveuncoveredthat 1351536=429·357. Exercise1.4.Explainwhythemethodlearnedbytheauthorsinschoolcorresponds tothecaseq=9.HinEt:10k mod9=1forallk≥0. Exercise1.5(Elferprobe,castingoutelevens).Powersoftenhaveverysimplere- maindersmodulo11,namely10k mod11=(−1)k forallk≥0,i.e.,1mod11=1, 10mod11=−1,100mod11=+1,1000mod11=−1,etc.Describeasimpletest tocheckthecorrectnessofamultiplicationmodulo11. C 1.4 A RecursiveVersionofthe School Method Weshallnowderivearecursiveversionoftheschoolmethod.Thiswillbeourfirst encounterwiththedivide-and-conquerparadigm,oneofthefundamentalparadigms O inalgorithmdesign. Letaandbbeourtwon-digitintegerswhichwewanttomultiply.Letk=⌊n/2⌋. Wesplitaintotwonumbersa anda ;a consistsofthekleastsignificantdigitsand 1 0 0 a consistsofthen−kmostsignificantdigits.8Wesplitbanalogously.Then 1 P a=a ·Bk+a and b=b ·Bk+b , 1 0 1 0 andhence a·b=a ·b ·B2k+(a ·b +a ·b )·Bk+a ·b . 1 1 1 0 0 1 0 0 Y Thisformulasuggeststhefollowingalgorithmforcomputinga·b: 7Themethodtaughtinschoolusesresiduesintherange1to9insteadof0to8accordingto thedefinitionsa=a−(⌈a/q⌉−1)·q. 8Observethatwehavechangednotation;a anda nowdenotethetwopartsofaandare 0 1 nolongersingledigits. 8 1 Appetizer:IntegerArithmetics (a) Splitaandbintoa ,a ,b ,andb . 1 0 1 0 (b) Computethefourproductsa ·b ,a ·b ,a ·b ,anda ·b . 1 1 1 0 0 1 0 0 (c) Addthesuitablyalignedproductstoobtaina·b. Observethatthenumbersa ,a ,b ,andb are⌈n/2⌉-digitnumbersandhencethe 1 0 1 0 Fmultiplicationsinstep(b)aresimplerthantheoriginalmultiplicationif⌈n/2⌉<n, i.e.,n>1.Thecompletealgorithmisnowasfollows.Tomultiplyone-digitnumbers, usethemultiplicationprimitive.Tomultiplyn-digitnumbersforn≥2,usethethree- stepapproachabove. ItRisclearwhythisapproachiscalleddivide-and-conquer.Wereducetheproblem of multiplying a and b to some number of simpler problems of the same kind. A divide-and-conqueralgorithmalwaysconsistsofthreeparts:inthefirstpart,wesplit the original problem into simpler problems of the same kind (our step (a)); in the secondpartwesolvethesimplerproblemsusingthesamemethod(ourstep(b));and, inthethirdpaErt,weobtainthesolutiontotheoriginalproblemfromthesolutionsto thesubproblems(ourstep(c)). a1 a0 Fig.1.2.Visualizationoftheschoolmethodand Eits recursive variant. The rhombus-shaped area . . indicatesthepartialproductsinthemultiplication a1 b0 a0 b0 b0 a·b.Thefoursubareascorrespondtothepartial productsa1·b1,a1·b0,a0·b1,anda0·b0.Inthe . . recursive scheme, wefirst sum thepartial prod- a1 b1 a0 b1 b1 ucts in the four subareas and then, in a second step,addthefourresultingsums C What is the connection of our recursive integer multiplication to the school method? It is really the same method. Figure 1.2 shows that the products a ·b , 1 1 a ·b ,a ·b ,anda ·b arealsocomputedintheschoolmethod.Knowingthatour 1 0 0 1 0 0 recursiveintegermultiplicationisjustthOeschoolmethodindisguisetellsusthatthe recursivealgorithmusesaquadraticnumberofprimitiveoperations.Letusalsode- rivethisfromfirstprinciples.Thiswillallowustointroducerecurrencerelations,a powerfulconceptfortheanalysisofrecursivealgorithms. Lemma1.4.Let T(n) be the maximal number oPf primitive operations required by ourrecursivemultiplicationalgorithmwhenappliedton-digitintegers.Then 1 ifn=1, T(n)≤ (4·T(⌈n/2⌉)+3·2·n ifYn≥2. Proof. Multiplying two one-digit numbers requires one primitive multiplication. Thisjustifiesthecasen=1.So,assumen≥2.Splittingaandbintothefourpieces a , a , b , and b requires no primitive operations.9 Each piece has at most ⌈n/2⌉ 1 0 1 0 9Itwillrequirework,butitisworkthatwedonotaccountforinouranalysis. 1.5 KaratsubaMultiplication 9 digitsandhencethefourrecursivemultiplicationsrequireatmost4·T(⌈n/2⌉)prim- itive operations.Finally, we need three additionsto assemble the final result. Each addition involves two numbers of at most 2n digits and hence requires at most 2n primitiveoperations.Thisjustifiestheinequalityforn≥2. ⊓⊔ FInSect.2.6,weshalllearnthatsuchrecurrencesareeasytosolveandyieldthe alreadyconjecturedquadraticexecutiontimeoftherecursivealgorithm. Lemma1.5.Let T(n) be the maximal number of primitive operations required by ourrRecursivemultiplicationalgorithmwhenappliedton-digitintegers.ThenT(n)≤ 7n2ifnisapoweroftwo,andT(n)≤28n2foralln. Proof. WereferthereadertoSect.1.8foraproof. ⊓⊔ E 1.5 Karatsuba Multiplication In1962,theSovietmathematicianKaratsuba[104]discoveredafasterwayofmulti- plyinglargeintegers.Therunningtimeofhisalgorithmgrowslikenlog3≈n1.58.The E methodissurprisinglysimple.Karatsubaobservedthatasimplealgebraicidentityal- lowsonemultiplicationtobeeliminatedinthedivide-and-conquerimplementation, i.e.,onecanmultiplyn-bitnumbersusingonlythreemultiplicationsofintegershalf thesize. Thedetailsareasfollows.Letaandbbeourtwon-digitintegerswhichwewant to multiply. Let k=⌊n/2⌋. As above, we split a into two numbers a and a ; a 1 0 0 consistsofthek leastsignificanCtdigitsanda consistsofthen−kmostsignificant 1 digits.Wesplitbinthesameway.Then a=a ·Bk+a and b=b ·Bk+b 1 0 1 0 andhence(themagicisinthesecondeqOuality) a·b=a ·b ·B2k+(a ·b +a ·b )·Bk+a ·b 1 1 1 0 0 1 0 0 =a ·b ·B2k+((a +a )·(b +b )−(a ·b +a ·b ))·Bk+a ·b . 1 1 1 0 1 0 1 1 0 0 0 0 Atfirstsight,wehaveonlymadethingsmorePcomplicated.Asecondlook,how- ever, shows that the last formula can be evaluated with only three multiplications, namely,a ·b ,a ·b ,and(a +a )·(b +b ).We alsoneedsixadditions.10 That 1 1 1 0 1 0 1 0 is three more than in the recursive implementation of the school method. The key isthatadditionsarecheapcomparedwithmultiplicationYs,andhencesavinga mul- tiplicationmorethanoutweighsthreeadditionaladditions.We obtainthefollowing algorithmforcomputinga·b: 10Actually,fiveadditionsandonesubtraction.Weleaveittoreaderstoconvincethemselves thatsubtractionsarenoharderthanadditions. 10 1 Appetizer:IntegerArithmetics (a) Splitaandbintoa ,a ,b ,andb . 1 0 1 0 (b) Computethethreeproducts p =a ·b , p =a ·b , p =(a +a )·(b +b ). 2 1 1 0 0 0 1 1 0 1 0 F(c) Addthesuitablyalignedproductstoobtaina·b,i.e.,computea·baccordingto theformula a·b=p ·B2k+(p −(p +p ))·Bk+p . 2 1 2 0 0 The numbers a , a , b , b , a +a , and b +b are ⌈n/2⌉+1-digit numbers and 1 0 1 0 1 0 1 0 R hence the multiplications in step (b) are simpler than the original multiplication if ⌈n/2⌉+1<n, i.e., n≥4. The complete algorithm is now as follows: to multiply three-digitnumbers,usetheschoolmethod,andtomultiplyn-digitnumbersforn≥ 4,usethethree-stepapproachabove. E school method Karatsuba4 10 Karatsuba32 E 1 0.1 ec] Fig.1.3.Therunning timesof implemen- s e [ 0.01 CtationsoftheKaratsubaandschool meth- m ti ods for integer multiplication. The run- ningtimesfortwoversionsofKaratsuba’s 0.001 methodareshown:Karatsuba4switchesto theschool methodforintegerswithfewer thanfourdigits,andKaratsuba32switches 0.0001 O to the school method for integers with fewer than 32 digits. The slopes of the 1e-05 linesfortheKaratsubavariantsareapprox- imately 1.58. The running time of Karat- 24 26 28 210 212 214 suba32isapproximatelyone-thirdtherun- n ningPtimeofKaratsuba4. Figure 1.3 shows the running times T (n) and T (n) of C++ implementations K S of the Karatsuba methodand the schoolmethodforn-digitintegers. The scales on Y bothaxesare logarithmic.We see, essentially,straightlinesof differentslope.The running time of the school method grows like n2, and hence the slope is 2 in the caseoftheschoolmethod.TheslopeissmallerinthecaseoftheKaratsubamethod andthissuggeststhatitsrunningtimegrowslikenb withb <2.Infact,theratio11 T (n)/T (n/2) is close to three, and this suggests that b is such that 2b = 3 or K K 11TK(1024)=0.0455,TK(2048)=0.1375,andTK(4096)=0.41.
Description: