{VERSION 6 0 "IBM INTEL NT" "6.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 1 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 1 }{CSTYLE "2D Comment" 2 18 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "2D Output" 2 20 "" 0 1 0 0 255 1 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 256 "" 1 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 258 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 260 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 261 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 262 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 263 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 264 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 265 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 266 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 267 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 268 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 269 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 1 " -1 3 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 4 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 2" -1 4 1 {CSTYLE "" -1 -1 "Times" 1 14 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 2 1 0 1 0 2 2 0 1 }{PSTYLE "Warning" -1 7 1 {CSTYLE "" -1 -1 "Courier" 1 10 0 0 255 1 2 2 2 2 2 1 1 1 3 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Error" -1 8 1 {CSTYLE "" -1 -1 "Courier" 1 10 255 0 255 1 2 2 2 2 2 1 1 1 3 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Maple Output" -1 11 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }3 3 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Maple Output" -1 12 1 {CSTYLE "" -1 -1 "Ti mes" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }1 3 0 0 0 0 1 0 1 0 2 2 0 1 } {PSTYLE "Maple Plot" -1 13 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }3 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Title" -1 18 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 1 2 2 2 1 1 1 1 }3 1 0 0 12 12 1 0 1 0 2 2 19 1 }{PSTYLE "Author" -1 19 1 {CSTYLE "" -1 -1 "Tim es" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }3 1 0 0 8 8 1 0 1 0 2 2 0 1 }} {SECT 0 {EXCHG {PARA 18 "" 0 "" {TEXT -1 36 "Modular Arithmetic and Fa st Powering" }}{PARA 19 "" 0 "" {TEXT -1 14 "Jeremy Johnson" }}{PARA 0 "" 0 "" {TEXT -1 369 "This worksheet contains various computations w ith modular numbers. Basic arithmetic is reviewed, including the comp utation of modular inverses with the extended Euclidean algorithm and \+ Fermat's theorem, fast algorithms for computing powers, and the Chines e Remainder theorem. The worksheet concludes with a modular algorithm for computing the product of two integers." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "with(numtheory); " }}{PARA 7 "" 1 "" {TEXT -1 69 "Warning, the protected name order has been redefined and unprotected\n" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#7 Q%&GIgcdG%)bigomegaG%&cfracG%)cfracpolG%+cyclotomicG%)divisorsG%)facto rEQG%*factorsetG%'fermatG%)imagunitG%&indexG%/integral_basisG%)invcfra cG%'invphiG%*issqrfreeG%'jacobiG%*kroneckerG%'lambdaG%)legendreG%)mcom bineG%)mersenneG%(migcdexG%*minkowskiG%(mipolysG%%mlogG%'mobiusG%&mroo tG%&msqrtG%)nearestpG%*nthconverG%)nthdenomG%)nthnumerG%'nthpowG%&orde rG%)pdexpandG%$phiG%#piG%*pprimrootG%)primrootG%(quadresG%+rootsunityG %*safeprimeG%&sigmaG%*sq2factorG%(sum2sqrG%$tauG%%thueG" }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 18 "Modular Arithmetic" }}{PARA 0 "" 0 "" {TEXT -1 165 "Use mod function after every arithmetic operation to alw ays get the representative equivalent to the result mod m. By default the positive representatives are used." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "3 + 3 mod 5;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "3*3 mod 5;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"%" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 47 "1/2 mod 5; # see sectio n on modular inverses." }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"$" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "3 * 2 mod 5;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 32 "For the symmetric form use mods." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "mods(4,5);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#!\"\"" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 423 "The integers mod m, denoted by Z_m, is the set of equivalence classes mod m. Two numb ers are equivalent mod m if they differ by a multiple of m (alternativ ely they have the same remainder when divided by m). The equivalence \+ classes mod m are denoted by [0], [1],...,[m-1], where [a] is the set \+ of integers that have the same remainder as the representative a. Not e that [a] and [a+q*m] denote the same equivalence class." }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 168 "The classes mod 3 , are [0], [1], [2]. [0] is the set of all multiples of 3 = \{3*q, q \+ an integer\}, [1] is the set \{3*q+1,q an integer\}, and [2] = \{3*q+2 , q an integer\}." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "seq(a mod 3, a=-9..9);" }}{PARA 11 "" 1 "" {XPPMATH 20 "65\"\"!\"\"\"\"\"#F#F$F%F#F $F%F#F$F%F#F$F%F#F$F%F#" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 116 "Arithmetic can be performed in Z_m by performing \+ arithmetic on the representatives of the equivalence classes. I.E." } }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 17 "[a] + [b ] = [a+b]" }}{PARA 0 "" 0 "" {TEXT -1 17 "[a] * [b] = [a*b]" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 106 "This defininit ion is well defined since you get the same answer no matter what repre sentatives are chosen." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 11 "E.G. in Z_3" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 37 "[1] + [1] = [2] = [4] + [7] = [11]." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 253 "The mod \+ function is a homomorphism from the integers Z onto Z_m. I.E. you get the same result if you first perform an arithmetic operation and then compute the remainder, or if you first compute the remainders and the n perform the arithmetic operation." }}{PARA 0 "" 0 "" {TEXT -1 0 "" } }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 36 "10*5 mod 3 = (10 mod 3) * ( 5 mod 3);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#/\"\"#F$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 46 "10 + 5 mod 3 = ((10 mod 3) + (5 mod 3)) mod 3;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#/\"\"!F$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "with(linalg):" }}{PARA 7 "" 1 "" {TEXT -1 80 "Warning, the protected names norm and trace have been red efined and unprotected\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 " m := 5;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"mG\"\"&" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "A := matrix(m,m);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"AG-%&arrayG6%;\"\"\"\"\"&F(7\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "for a from 0 to m-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " for b from 0 to m-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " A[a+1,b+1] := a+b mod m;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 " end do;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "end do: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "print(A);" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#K%'matrixG6#7'7'\"\"!\"\"\"\"\"#\"\"$\"\"%7'F)F* F+F,F(7'F*F+F,F(F)7'F+F,F(F)F*7'F,F(F)F*F+Q(pprint26\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "M := matrix(m,m);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"MG-%&arrayG6%;\"\"\"\"\"&F(7\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "for a from 0 to m-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " for b from 0 to m-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " M[a+1,b+1] := a*b mod m;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 " end do;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "end do: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "print(M);" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#K%'matrixG6#7'7'\"\"!F(F(F(F(7'F(\"\"\"\"\"#\"\" $\"\"%7'F(F+F-F*F,7'F(F,F*F-F+7'F(F-F,F+F*Q(pprint36\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 28 "Extended Euclidean Algorithm" }}{PARA 0 "" 0 "" {TEXT -1 0 "" } }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "igcdex(3,5,'x','y');" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "x; y;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"#" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#!\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "3*x+5*y;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "# Extended Euclidean Algori thm." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 58 "mygcdex := proc(a::nonnegin t,b::nonnegint,x::name,y::name)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 37 " local a1,a2,a3,x1,x2,x3,y1,y2,y3,q;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " a1 := a; a2 := b;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 22 " x1 \+ := 1; y1 := 0; " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " x2 := 0; y2 := 1;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " while (a2 <> 0) do" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " a3 := a1 mod a2;" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 22 " q := floor(a1/a2);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " x3 := x1 - q*x2;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " y3 := y1 - q*y2;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " a 1 := a2; a2 := a3;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " x1 := x2 ; x2 := x3;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " y1 := y2; y2 : = y3;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 5 " od;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " x := x1; y := y1;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 12 " return a1;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%(mygcdexGf*6&'%\"aG%*nonnegintG'%\"bGF)'% \"xG%%nameG'%\"yGF.6,%#a1G%#a2G%#a3G%#x1G%#x2G%#x3G%#y1G%#y2G%#y3G%\"q G6\"F8$9$>8%9%>8'\"\"\">8*\"\"!>8(FI>8+FF?(F8&-%$mo dG6$F?FB>8--%&floorG6#*&F?FFFB!\"\">8),&FEFF*&FWFFFKFFFfn>8,,&FHFF*&FW FFFMFFFfn>F?FB>FBFR>FEFK>FKFhn>FHFM>FMF\\o>9&FE>9'FHOF?F " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 4 "" 0 "" {TEXT 256 16 "Computin g Powers" }}{PARA 0 "" 0 "" {TEXT -1 77 "Several recursive and iterati ve functions for computing powers are presented." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "Powerit := p roc(M,e)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 " local i,nu;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 30 " if (e = 0) then return 1 fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " nu := M;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " for i from 1 to e-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 " nu := nu*M;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 5 " od;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 12 " return nu;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%(PoweritGf*6 $%\"MG%\"eG6$%\"iG%#nuG6\"F,C&@$/9%\"\"!O\"\"\">8%9$?(8$F3F3,&F0F3F3! \"\"%%trueG>F5*&F5F3F6F3OF5F,F,F," }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "Powerit(2,0);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\" \"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "Powerrec1 := proc(M,e )" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 30 " if e = 0 then return 1 fi; \+ " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " return M*Powerrec1(M,e-1);" } }{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%*Powerrec1Gf*6$%\"MG%\"eG6\"F)F)C$@$/9%\"\"!O\"\"\"O*&9$F0-F$6 $F3,&F-F0F0!\"\"F0F)F)F)" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "Powerrec1(2,5);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#K" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "Powerrec := proc(M,e)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " local P;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " if e = 0 then return 1 fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 30 " P := Powerrec(M,floor(e/2));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 " P := P * P;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 35 " if type(e,odd) t hen P := P*M; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 " return P;" } }{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%)PowerrecGf*6$%\"MG%\"eG6#%\"PG6\"F+C'@$/9%\"\"!O\"\"\">8$-F$6 $9$-%&floorG6#,$*&#F2\"\"#F2F/F2F2>F4*&F4F2F4F2@$-%%typeG6$F/%$oddG>F4 *&F4F2F7F2OF4F+F+F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "Powe rrec(2,10);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"%C5" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "Powerrec(3,100) mod 100;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "Powerrec(3,100);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"Q,?_2@qs7il( H6YO5L6?t?vP:&" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "Powerrecm od := proc(M,e,n)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " local P;" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " if e = 0 then return 1 fi;" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 35 " P := Powerrecmod(M,floor(e/2),n); " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 19 " P := P * P mod n;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 41 " if type(e,odd) then P := P*M mod n; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 " return P;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%,Powerrecmod Gf*6%%\"MG%\"eG%\"nG6#%\"PG6\"F,C'@$/9%\"\"!O\"\"\">8$-F$6%9$-%&floorG 6#,$*&#F3\"\"#F3F0F3F39&>F5-%$modG6$*&F5F3F5F3F@@$-%%typeG6$F0%$oddG>F 5-FC6$*&F5F3F8F3F@OF5F,F,F," }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "Powerrecmod(3,5,7);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"&" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "Powerrec(3,5) mod 7;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"&" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 35 "time(Powerrec(3,1000000) mod 1023);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#$\"$^&!\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 34 "time(Powerrecmod(3,1000000,1023));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#$\"\"!F$" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 169 "In Maple, powe rs are computed using the ^ notation. If you want to incorporate the \+ mod function into the computation of powers you need to use the inert \+ function Power." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "3^100;" } }{PARA 11 "" 1 "" {XPPMATH 20 "6#\"Q,?_2@qs7il(H6YO5L6?t?vP:&" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "Power(3,100) mod 100;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 27 "Analysis of Binary Powering" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "PowerRec := proc(a,e)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " local y;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " if (e = 0) \+ then return 1; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " if (e = 1) \+ then return a; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " y := PowerR ec(a,floor(e/2)); " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 " y := y*y;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 37 " if (e mod 2) = 1 then y := y*a; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 " return y; " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%)Pow erRecGf*6$%\"aG%\"eG6#%\"yG6\"F+C(@$/9%\"\"!O\"\"\"@$/F/F2O9$>8$-F$6$F 6-%&floorG6#,$*&#F2\"\"#F2F/F2F2>F8*&F8F2F8F2@$/-%$modG6$F/FAF2>F8*&F8 F2F6F2OF8F+F+F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "PowerRec (2,10);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"%C5" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "PowerRec(2,15);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"&oF$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "PowerRecCou nt := proc(a,e)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 14 " local count;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " if (e = 0) then return 0; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " if (e = 1) then return 0; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 40 " count := PowerRecCount(a,floor( e/2)); " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 " count := count + 1;" } }{PARA 0 "> " 0 "" {MPLTEXT 1 0 47 " if (e mod 2) = 1 then count := c ount + 1; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 17 " return count; \+ " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%.PowerRecCountGf*6$%\"aG%\"eG6#%&countG6\"F+C(@$/9%\" \"!OF0@$/F/\"\"\"OF0>8$-F$6$9$-%&floorG6#,$*&#F4\"\"#F4F/F4F4>F7,&F7F4 F4F4@$/-%$modG6$F/FAF4>F7FCOF7F+F+F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "PowerRecCount(a,8);" }}{PARA 11 "" 1 "" {XPPMATH 20 " 6#\"\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "PowerRecCount(a, 9);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"%" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 47 "data := [seq([e,PowerRecCount(a,e)],e=0..128)]: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "with(plots):" }}{PARA 7 "" 1 "" {TEXT -1 50 "Warning, the name changecoords has been redefin ed\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "pointplot(data,styl e=LINE);" }}{PARA 13 "" 1 "" {GLPLOT2D 486 486 486 {PLOTDATA 2 "6$-%'C URVESG6#7]s7$$\"\"!F)F(7$$\"\"\"F)F(7$$\"\"#F)F+7$$\"\"$F)F.7$$\"\"%F) F.7$$\"\"&F)F17$$\"\"'F)F17$$\"\"(F)F47$$\"\")F)F17$$\"\"*F)F47$$\"#5F )F47$$\"#6F)F77$$\"#7F)F47$$\"#8F)F77$$\"#9F)F77$$\"#:F)F:7$$\"#;F)F47 $$\"#F)F:7$$\"#?F)F77$$\"#@F)F:7$$\"#AF)F:7$$\" #BF)F=7$$\"#CF)F77$$\"#DF)F:7$$\"#EF)F:7$$\"#FF)F=7$$\"#GF)F:7$$\"#HF) F=7$$\"#IF)F=7$$\"#JF)F@7$$\"#KF)F77$$\"#LF)F:7$$\"#MF)F:7$$\"#NF)F=7$ $\"#OF)F:7$$\"#PF)F=7$$\"#QF)F=7$$\"#RF)F@7$$\"#SF)F:7$$\"#TF)F=7$$\"# UF)F=7$$\"#VF)F@7$$\"#WF)F=7$$\"#XF)F@7$$\"#YF)F@7$$\"#ZF)FC7$$\"#[F)F :7$$\"#\\F)F=7$$\"#]F)F=7$$\"#^F)F@7$$\"#_F)F=7$$\"#`F)F@7$$\"#aF)F@7$ $\"#bF)FC7$$\"#cF)F=7$$\"#dF)F@7$$\"#eF)F@7$$\"#fF)FC7$$\"#gF)F@7$$\"# hF)FC7$$\"#iF)FC7$$\"#jF)FF7$$\"#kF)F:7$$\"#lF)F=7$$\"#mF)F=7$$\"#nF)F @7$$\"#oF)F=7$$\"#pF)F@7$$\"#qF)F@7$$\"#rF)FC7$$\"#sF)F=7$$\"#tF)F@7$$ \"#uF)F@7$$\"#vF)FC7$$\"#wF)F@7$$\"#xF)FC7$$\"#yF)FC7$$\"#zF)FF7$$\"#! )F)F=7$$\"#\")F)F@7$$\"##)F)F@7$$\"#$)F)FC7$$\"#%)F)F@7$$\"#&)F)FC7$$ \"#')F)FC7$$\"#()F)FF7$$\"#))F)F@7$$\"#*)F)FC7$$\"#!*F)FC7$$\"#\"*F)FF 7$$\"##*F)FC7$$\"#$*F)FF7$$\"#%*F)FF7$$\"#&*F)FI7$$\"#'*F)F=7$$\"#(*F) F@7$$\"#)*F)F@7$$\"#**F)FC7$$\"$+\"F)F@7$$\"$,\"F)FC7$$\"$-\"F)FC7$$\" $.\"F)FF7$$\"$/\"F)F@7$$\"$0\"F)FC7$$\"$1\"F)FC7$$\"$2\"F)FF7$$\"$3\"F )FC7$$\"$4\"F)FF7$$\"$5\"F)FF7$$\"$6\"F)FI7$$\"$7\"F)F@7$$\"$8\"F)FC7$ $\"$9\"F)FC7$$\"$:\"F)FF7$$\"$;\"F)FC7$$\"$<\"F)FF7$$\"$=\"F)FF7$$\"$> \"F)FI7$$\"$?\"F)FC7$$\"$@\"F)FF7$$\"$A\"F)FF7$$\"$B\"F)FI7$$\"$C\"F)F F7$$\"$D\"F)FI7$$\"$E\"F)FI7$$\"$F\"F)FL7$$\"$G\"F)F=-%&STYLEG6#%%LINE G" 1 6 0 1 10 0 2 9 1 4 2 1.000000 45.000000 45.000000 0 0 "Curve 1" } }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "counts := seq(PowerRecCou nt(a,e),e=8..16);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'countsG6+\"\"$ \"\"%F'\"\"&F'F(F(\"\"'F'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "seq(convert(e,binary),e=8..16);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6 +\"%+5\"%,5\"%55\"%65\"%+6\"%,6\"%56\"%66\"&++\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "numbits := proc(n)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 " local count, nb;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 23 " count := 0; nb := n;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " wh ile (nb <> 0) do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 48 " if nb mod 2 = 1 then count := count + 1; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 22 " nb := floor(nb/2);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 5 " od; " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 " return count;" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%(numb itsGf*6#%\"nG6$%&countG%#nbG6\"F+C&>8$\"\"!>8%9$?(F+\"\"\"F4F+0F1F/C$@ $/-%$modG6$F1\"\"#F4>F.,&F.F4F4F4>F1-%&floorG6#,$*&#F4F " 0 "" {MPLTEXT 1 0 24 "seq(numbits(e),e=8.. 16);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6+\"\"\"\"\"#F$\"\"$F$F%F%\"\"%F #" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "counts := seq(PowerRec Count(a,e),e=8..16);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'countsG6+\" \"$\"\"%F'\"\"&F'F(F(\"\"'F'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 64 "counts2 := seq(floor(simplify(log[2](e)))+numbits(e)-1,e=8..16); " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%(counts2G6+\"\"$\"\"%F'\"\"&F'F( F(\"\"'F'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 75 "seq(PowerRecCo unt(a,e)-(floor(simplify(log[2](e)))+numbits(e)-1),e=1..128);" }} {PARA 12 "" 1 "" {XPPMATH 20 "6\\s\"\"!F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F #F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F #F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F #F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F#F #F#F#F#F#F#F#" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}} {SECT 1 {PARA 3 "" 0 "" {TEXT -1 17 " Modular Inverses" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 525 "A modular inverse o f a in Z_m is an element b of Z_m such that a*b = 1 mod m. Not all el ements of Z_m have inverses. a in Z_m has an inverse if and only if g cd(a,m) = 1. If gcd(a,m) = 1, then the inverse of a can be found with the extended Euclidean algorithm, since x and y can be found such tha t a*x + b*y = 1. Taking the left and right sides mod m implies that a *x = 1 mod m. If m is a prime, then all of the non-zero elements have inverses. In general the number of elements of Z_m with inverses is \+ equal to phi(m)." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 52 "Inverses can also be computed with Euler's identity." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 470 "If gcd(a ,m) <> 1, then there exists x and y such that a*x + m*y = g = gcd(a,m) , which implies that a*x*(m/g) = 0 mod m (i.e. a is a zero divisor - t here exist a non-zero element b of Z_m such that a*b = 0 mod m). If a were to have an inverse, then multiplying both sides of a*b=0 mod m, \+ implies b = 0 which is a contradiction). Therefore, the only elements of Z_m that have inverses are those elements a such that gcd(a,m) = 1 . There are exactly phi(m) such elements." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "checkinv := proc(a ,m)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " local i;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 " for i from 1 to m-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 42 " if i*a mod m = 1 then return true; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 " end do;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 " return false;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%)checkinvGf*6$%\"aG%\"mG6#%\"iG6\"F+ C$?(8$\"\"\"F/,&9%F/F/!\"\"%%trueG@$/-%$modG6$*&F.F/9$F/F1F/OF3O%&fals eGF+F+F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 29 "count := 0; m : = 17; phi(m);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&countG\"\"!" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"mG\"#<" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "for \+ a from 0 to m-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 35 " print(a,che ckinv(a,m),igcd(a,m));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 47 " if chec kinv(a,m) then count := count + 1; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"!%&falseG\"#<" }} {PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"\"%%trueGF#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"#%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6% \"\"$%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"%%%trueG\"\" \"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"&%%trueG\"\"\"" }}{PARA 11 " " 1 "" {XPPMATH 20 "6%\"\"'%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"(%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\")%%tru eG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"*%%trueG\"\"\"" }} {PARA 11 "" 1 "" {XPPMATH 20 "6%\"#5%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#6%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\" #7%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#8%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#9%%trueG\"\"\"" }}{PARA 11 "" 1 " " {XPPMATH 20 "6%\"#:%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6% \"#;%%trueG\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "count \+ = phi(m);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#/\"#;F$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 29 "count := 0; m := 15; phi(m);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&countG\"\"!" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"mG\"#:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\")" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "for a from 0 to m-1 do" }}{PARA 0 " > " 0 "" {MPLTEXT 1 0 35 " print(a,checkinv(a,m),igcd(a,m));" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 47 " if checkinv(a,m) then count := co unt + 1; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"!%&falseG\"#:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"\"%%trueGF#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"#%%trueG\"\" \"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"$%&falseGF#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"%%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"&%&falseGF#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"'%&falseG \"\"$" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"(%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\")%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"\"*%&falseG\"\"$" }}{PARA 11 "" 1 "" {XPPMATH 20 "6% \"#5%&falseG\"\"&" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#6%%trueG\"\"\" " }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#7%&falseG\"\"$" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#8%%trueG\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6%\"#9%%trueG\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "cou nt = phi(m);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#/\"\")F$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "igcdex(3, 17,'x','y');" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "x mod 17;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "3 * x mod 17;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "Po wer(3,15) mod 17;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"'" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 25 "Chinese Remainder Theorem" }}{PARA 0 " " 0 "" {TEXT -1 105 "The Chinese Remainder Theorem (CRT) says that the re exists an integer solution to the system of equations" }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 11 "x = a mod m" }} {PARA 0 "" 0 "" {TEXT -1 11 "x = b mod n" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 236 "for any a and b, whenever gcd(m,n ) = 1. The solution is guaranteed by considering the map from Z -> Z_ m X Z_n given by x -> [ mod m, x mod n], since the numbers 0,...,m*n-1 map to different pairs (since m and n are relatively prime). " }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "m := 3; n := 5; Z15 := \{\};" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#> %\"mG\"\"$" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"nG\"\"&" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%$Z15G<\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "for x from 0 to m*n-1 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " print(x,[x mod m, x mod n]);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 40 " Z15 := Z15 union \{[x mod m, x mod n]\};" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\" \"!7$F#F#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"\"\"7$F#F#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"\"#7$F#F#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$ \"\"$7$\"\"!F#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"\"%7$\"\"\"F#" }} {PARA 11 "" 1 "" {XPPMATH 20 "6$\"\"&7$\"\"#\"\"!" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"\"'7$\"\"!\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$ \"\"(7$\"\"\"\"\"#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"\")7$\"\"#\"\" $" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"\"*7$\"\"!\"\"%" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"#57$\"\"\"\"\"!" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"#67$\"\"#\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"#77$\"\"!\" \"#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$\"#87$\"\"\"\"\"$" }}{PARA 11 " " 1 "" {XPPMATH 20 "6$\"#97$\"\"#\"\"%" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "nops(Z15);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#:" } }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 116 "This s ays that Z_\{m*n\} = Z_m X Z_n. Moreover, this mapping is an isomorph ism (i.e. it preserves arrithmetic). Let " }{XPPEDIT 18 0 "pi(x) = [x mod m, x mod n]" "6#/-%#piG6#%\"xG7$-%$modG6$F'%\"mG-F*6$F'%\"nG" } {TEXT -1 8 ". Then " }{XPPEDIT 18 0 "pi(x+y) = pi(x) + pi(y)" "6#/-%# piG6#,&%\"xG\"\"\"%\"yGF),&-F%6#F(F)-F%6#F*F)" }{TEXT -1 5 " and " } {XPPEDIT 18 0 "pi(x*y) = pi(x)*pi(y);" "6#/-%#piG6#*&%\"xG\"\"\"%\"yGF )*&-F%6#F(F)-F%6#F*F)" }{TEXT -1 59 ", where arithmetic in Z_m X Z_n i s performed componentwise." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "8 * 7 mod 15;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#6" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "8 + \+ 7 mod 15;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "[8 mod 3, 8 mod 5];" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#7$\"\"#\"\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "[7 mod 3, 7 mod 5];" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#7$\"\"\"\" \"#" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "[11 mod 3, 11 mod 5] ;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#7$\"\"#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "[0 mod 3, 0 mod 5];" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#7$\"\"!F$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 69 "[8 mod 3, 8 mod 5] + [7 mod 3, 7 mod 5] = [8 + 7 mod 3, 8 + 7 mod 5]; " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#/7$\"\"$\"\"&7$\"\"!F(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 69 "[8 mod 3, 8 mod 5] * [7 mod 3, 7 mo d 5] = [8 * 7 mod 3, 8 * 7 mod 5];" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# /*&7$\"\"#\"\"$\"\"\"7$F(F&F(7$F&F(" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 257 "So far, we have only shown that a solu tion to the Chinese Remainder problem exists and can be found by searc h. A constructive solution, based on the Extended Euclidean algorithm , can be used to avoid search. Maple implements the CRT in the functi on chrem." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "chrem([2,1],[3,5]);" }}{PARA 11 "" 1 "" {XPPMATH 20 " 6#\"#6" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 169 "chrem can be used for more than two moduli. The theorem extends \+ to t moduli [n1,...nt] where the moduli are pairwise relatively prime. I.E. gcd(ni,nj) = 1 for i <> j." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 23 "chrem([2,1,3],[3,5,7]);" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"$,\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 32 "101 mod 3; 101 mod 5; 101 mod 7;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"$" }}}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{PARA 0 "" 0 "" {TEXT -1 133 "To see how the CRT can be implemente d using the extended Euclidean algorithm, recall that if gcd(m,n) = 1, then there exist integers " }{XPPEDIT 18 0 "lambda" "6#%'lambdaG" } {TEXT -1 5 " and " }{XPPEDIT 18 0 "mu" "6#%#muG" }{TEXT -1 11 " such t hat " }{XPPEDIT 18 0 "lambda*m+mu*n = 1;" "6#/,&*&%'lambdaG\"\"\"%\"mG F'F'*&%#muGF'%\"nGF'F'F'" }{TEXT -1 7 ". Let " }{XPPEDIT 18 0 "e[m] = mu*n;" "6#/&%\"eG6#%\"mG*&%#muG\"\"\"%\"nGF*" }{TEXT -1 5 " and " } {XPPEDIT 18 0 "e[n] = lambda*m;" "6#/&%\"eG6#%\"nG*&%'lambdaG\"\"\"%\" mGF*" }{TEXT -1 8 ". Then " }{XPPEDIT 18 0 "e[m] = 1 mod m" "6#/&%\"e G6#%\"mG-%$modG6$\"\"\"F'" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "e[m] = \+ 0 mod n" "6#/&%\"eG6#%\"mG-%$modG6$\"\"!%\"nG" }{TEXT -1 14 ". Simila rly, " }{XPPEDIT 18 0 "e[n] = 0 mod m" "6#/&%\"eG6#%\"nG-%$modG6$\"\"! %\"mG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "e[n] = 1 mod n" "6#/&%\"eG6 #%\"nG-%$modG6$\"\"\"F'" }{TEXT -1 44 ". Therefore, if we want to fin d an integer " }{XPPEDIT 18 0 "x" "6#%\"xG" }{TEXT -1 23 " that is equ ivalent to " }{XPPEDIT 18 0 "a mod m" "6#-%$modG6$%\"aG%\"mG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "b mod n" "6#-%$modG6$%\"bG%\"nG" }{TEXT -1 25 ", then we can simply set " }{XPPEDIT 18 0 "x = e[m]*a + e[n]*b " "6#/%\"xG,&*&&%\"eG6#%\"mG\"\"\"%\"aGF+F+*&&F(6#%\"nGF+%\"bGF+F+" } {TEXT -1 33 ". Using the above properties of " }{XPPEDIT 18 0 "e[m]" "6#&%\"eG6#%\"mG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "e[n]" "6#&%\"eG6 #%\"nG" }{TEXT -1 25 ", it is easy to see that " }{XPPEDIT 18 0 "x = a mod m" "6#/%\"xG-%$modG6$%\"aG%\"mG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "x = b mod n" "6#/%\"xG-%$modG6$%\"bG%\"nG" }{TEXT -1 1 "." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 49 "This cons truction can be generalized to t moduli " }{XPPEDIT 18 0 "[n[1]..n[t]] " "6#7#;&%\"nG6#\"\"\"&F&6#%\"tG" }{TEXT -1 134 " that are pairwise re latively prime. This generalization is similar the the Lagrange inter polation formula. The key is to construct " }{XPPEDIT 18 0 "e[1]" "6# &%\"eG6#\"\"\"" }{TEXT -1 4 ",..," }{XPPEDIT 18 0 "e[t]" "6#&%\"eG6#% \"tG" }{TEXT -1 11 " such that " }{XPPEDIT 18 0 "e[i] = 1 mod n[i]" "6 #/&%\"eG6#%\"iG-%$modG6$\"\"\"&%\"nG6#F'" }{TEXT -1 5 " and " } {XPPEDIT 18 0 "e[i] = 0 mod n[j]" "6#/&%\"eG6#%\"iG-%$modG6$\"\"!&%\"n G6#%\"jG" }{TEXT -1 5 " for " }{XPPEDIT 18 0 "i <> j" "6#0%\"iG%\"jG" }{TEXT -1 1 "." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "m := 3; n := 5;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"mG\"\"$" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"nG\"\"&" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "igcdex(m,n,'lambda','mu');" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "lambda;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"#" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "mu;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#!\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "lam bda*m + mu*n;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 29 "e[n] := lambda * m mod (m*n);" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>&%\"eG6#\"\"&\"\"'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 25 "e[m] := mu * n mod (m*n);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>&%\"eG6#\"\"$\"#5" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "e[m] mod m; e[m] mod n;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "e[n] mod m; e[n] mod n;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "a := 1; b := \+ 2;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"aG\"\"\"" }}{PARA 11 "" 1 " " {XPPMATH 20 "6#>%\"bG\"\"#" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "x := a*e[m] + b*e[n] mod (m*n);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"xG\"\"(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "x mod m = a; x mod n = b;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#/\"\"\"F$" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#/\"\"#F$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 44 "Modular Alg orithm for Integer Multiplication" }}{PARA 0 "" 0 "" {TEXT -1 469 "Thi s section provides a modular algorithm to multiply two integers. The a lgorithm is similar to the evaluate, pointwise product, interpolate al gorithm discussed in class to multiply polynomials. The algorithm wil l 1) reduce the input integers mod p for sufficiently many primes p, \+ 2) multiply each of the reductions mod p, 3) use the Chinese Remainder algorithm (you may use chrem) to find an integer that is equivalent t o all of the products of the reductions mod p." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 12 " An example." }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "a := \+ 123456;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"aG\"'cM7" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "b := 654321;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"bG\"'@Vl" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 257 2 "1)" }{TEXT -1 153 " Multiply c=a*b and determin e the number of digits in c. What is the relationship between the num ber of digits in c and the number of digits in a and b?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "c := a*b ;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"cG\",wL&)z2)" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 34 "length(a); length(b); length(c); " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"'" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"'" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#6" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 262 266 "In gene ral length(a) + length(b) - 1 <= length(a*b) <= length(a) + length(b). To see this note that if a has length m, then 10^(m-1) <= a < 10^m - 1. Therefore, if a has length m and b has length n, then 10^(m+n-2) \+ <= a*b < 10^(m+n), and the result follows. E.G." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "length(99999 9*999999);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#7" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "length(100000*100000);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#6" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 " " {TEXT 258 2 "2)" }{TEXT -1 152 " Find the three smallest primes grea ter than 10000. Hint use the function nextprime. What can you say ab out the length of the product of these primes?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 "p1 := nextpr ime(10000); p2 := nextprime(p1); p3 := nextprime(p2);" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#>%#p1G\"&2+\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6 #>%#p2G\"&4+\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#p3G\"&P+\"" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "length(p1*p2*p3);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"#8" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT 263 199 "Each prime has length 5 and by the prev ious part, the length of their product is greater than or equal to 3*5 -2 = 13. In general the product of t n digit primes will be atleast \+ t*n - (t-1) >= t*n." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 " " {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 259 2 "3)" }{TEXT -1 22 " Comp ute the products " }{XPPEDIT 18 0 "c[i] = a[i]*b[i] mod p[i]" "6#/&%\" cG6#%\"iG-%$modG6$*&&%\"aG6#F'\"\"\"&%\"bG6#F'F/&%\"pG6#F'" }{TEXT -1 5 " for " }{XPPEDIT 18 0 "i=1..3" "6#/%\"iG;\"\"\"\"\"$" }{TEXT -1 8 " , where " }{XPPEDIT 18 0 "a[i] = a mod p[i]" "6#/&%\"aG6#%\"iG-%$modG6 $F%&%\"pG6#F'" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "b[i] = b mod p[i]" "6#/&%\"bG6#%\"iG-%$modG6$F%&%\"pG6#F'" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "p[1], p[2], p[3]" "6%&%\"pG6#\"\"\"&F$6#\"\"#&F$6#\"\"$" }{TEXT -1 35 " are the three primes found in (2)." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "a1 := a mod p1; a 2 := a mod p2; a3 := a mod p3;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%# a1G\"%sL" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#a2G\"%[L" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#>%#a3G\"%7I" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "b1 := b mod p1; b2 := b mod p2; b3 := b mod p3;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%#b1G\"%mQ" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#b2G\"%OP" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#b3G\" %;>" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 61 "c1 := a1*b1 mod p1; \+ c2 := a2*b2 mod p2; c3 := a3*b3 mod p3;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#c1G\"%Qq" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#c2G\" %()o" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#c3G\"%a(*" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 260 2 "4)" }{TEXT -1 105 " Us e the Chinese Remainder algorithm (maple function chrem) to find the i nteger cp that is equivalent to " }{XPPEDIT 18 0 "c[1] mod p[1], c[2] \+ mod p[2]" "6$-%$modG6$&%\"cG6#\"\"\"&%\"pG6#F)-F$6$&F'6#\"\"#&F+6#F1" }{TEXT -1 6 ", and " }{XPPEDIT 18 0 "c[3] mod p[3]" "6#-%$modG6$&%\"cG 6#\"\"$&%\"pG6#F)" }{TEXT -1 28 ". How does cp compare to c?" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 35 "cp := chrem([c1,c2,c3],[p1,p2,p3]);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#cpG\",wL&)z2)" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "cp - c;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 261 2 "5)" }{TEXT -1 30 " If the product of the primes " }{XPPEDIT 18 0 "p[1], p[2], p[3]" "6%&%\"pG6# \"\"\"&F$6#\"\"#&F$6#\"\"$" }{TEXT -1 90 " was not greater than c, the n chrem would not return the c = a*b, but rather would return " } {XPPEDIT 18 0 "c mod p[1]*p[2]*p[3]" "6#-%$modG6$%\"cG*(&%\"pG6#\"\"\" F+&F)6#\"\"#F+&F)6#\"\"$F+" }{TEXT -1 6 ". If " }{XPPEDIT 18 0 "c < p [1]*p[2]*p[3];" "6#2%\"cG*(&%\"pG6#\"\"\"F)&F'6#\"\"#F)&F'6#\"\"$F)" } {TEXT -1 229 ", then the answer returned by chrem will be the same as \+ a*b. Demonstrate that c is not returned if the primes are too small, \+ by using smaller primes. Also verify that the answer returned is c mo d the product of the primes used." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 84 "cpp := chrem([(a mod 3)*(b mod 3),(a mod 5)*(b mod 5) ,(a mod 7)*(b mod 7)],[3,5,7]);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%$ cppG\"#'*" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "cpp - c mod (3 *5*7);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 658 "Implement a Maple proced ure that multiplies two integers a and b using an algorithm that gener alized the previous example. First compute a list of 100 primes great er than 10^5. Assign this list to a variable called PRIMES. Your mul tiplication procedure needs to first determine the smallest number of \+ primes in the list PRIMES required so that if chrem is used to find an integer that is equivalent to the product of a and b mod each of the \+ primes used the true product a*b is returned. Hint, find a bound on t he length of the product product of a and b and determine the number o f primes such that the length of there product is greater than this bo und." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 341 " After you have determined the number of primes required [if PRIMES doe s not have sufficient primes, exit with an error] reduce the inputs mo d each of these primes, multiply the reduced numbers mod the primes to obtain the answer c = a*b mod these primes, and finally use chrem to \+ find the integers that is equivalent to the c mod the primes." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 36 "PRIMES := []; p := nextprime(10^5);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'PRIMESG7\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"pG\"'.+5" } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "for i from 1 to 100 do" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 " PRIMES := [p,op(PRIMES)]:" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " p := nextprime(p):" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 3 "od:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "PRIMES;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#7`q\"'(>,\"\"'$=,\"\"'t6 5\"'h65\"'f65\"'\\65\"'T65\"'>65\"'<65\"'865\"'665\"'265\"'*3,\"\"'\"3 ,\"\"'j55\"'^55\"'F55\"'@55\"'455\"'**45\"'()45\"'\")45\"'d45\"'V45\"' P45\"'J45\"'F45\"'845\"'245\"'`35\"'Z35\"'H35\"'B35\"'635\"',35\"'*z+ \"\"'(y+\"\"'p25\"'Z25\"'T25\"'L25\"'.25\"'*p+\"\"'$p+\"\"'t15\"'p15\" '\\15\"'@15\"'815\"'415\"'\"f+\"\"'f05\"'\\05\"'Z05\"'P05\"'B05\"'>05 \"'<05\"'605\"',05\"'$\\+\"\"'$[+\"\"'p/5\"'f/5\"'Z/5\"'+\"\"'*=+\"\"'$=+\"\"' p,5\"'`,5\"'^,5\"'H,5\"'4,5\"'.,5\"'p+5\"'d+5\"'\\+5\"'V+5\"'>+5\"'.+5 " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 41 "# Iprod(A,B). Integer \+ product using CRT." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 35 "# Inputs: A, B : positive integers" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "# Output: C = A*B." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "Iprod := proc(A,B)" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 " local m, n, t, C;" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 16 " global PRIMES;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 38 "# Determine number of primes required." }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 34 " m := length(A); n := length(B);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 81 " t := ceil((m+n+1)/5); # Must have length (p1*...*pt) >= t*5 + (t-1) > m + n => " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 66 " if t > nops(PRIMES) then error \"Insufficiently many primes \"; fi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 35 "# Multiply A*B mod pi fo r t primes." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 71 " C := [seq((A mod P RIMES[i])*(B mod PRIMES[i]) mod PRIMES[i],i=1..t)];" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "# Use CRT to recover integer product. " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " return chrem(C,PRIMES[1..t]);;" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%&IprodGf*6$%\"AG%\"BG6&%\"mG%\"nG%\"tG%\"CG6\"F.C(>8$-%'length G6#9$>8%-F36#9%>8&-%%ceilG6#,(*&#\"\"\"\"\"&FCF1FCFC*&FBFCF7FCFCFBFC@$ 2-%%nopsG6#%'PRIMESGF8'7#-%$seqG6$-%$ modG6$*&-FU6$F5&FK6#%\"iGFC-FU6$F:FZFCFZ/Ffn;FCF " 0 "" {MPLTEXT 1 0 27 "C := 123456789 * 987654321;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#> %\"CG\"3p_j76jK>7" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "Iprod( 123456789,987654321);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"3p_j76jK>7 " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "NumSamples := 10;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%+NumSamplesG\"#5" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 29 "for d from 10 by 10 to 100 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " myrand := rand(1..10^d-1):" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " for i from 1 to NumSamples do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 34 " A := myrand(): B := myrand():" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 73 " if Iprod(A,B) <> A*B then error \"Iprod f ailed for %d and %d\",A,B; fi:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 " o d:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 265 19 "Test special cases." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "Ip rod(1,1);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "100*1000 - Iprod(100,1000);" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#\"\"!" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 23 "99*999 - Iprod(99,999);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 25 "999*999 - Iprod(999,999); " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"!" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 268 143 "What is the largest input tha t works, i.e. there are sufficiently many primes? With 100 five digit primes, we would expect roughly 500 digits." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "Iprod(10^250,10^25 0);" }}{PARA 8 "" 1 "" {TEXT -1 45 "Error, (in Iprod) Insufficiently m any primes\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "Iprod(10^25 0,10^248);" }}{PARA 8 "" 1 "" {TEXT -1 45 "Error, (in Iprod) Insuffici ently many primes\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "Ipro d(10^250,10^247);" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#\"]jl++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++5" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 96 "Empirically and theoretically determine the gro wth rate of the computing time of your procedure." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 266 290 "Let leng th(A) = length(B) = N, then t = ceil((2*N+1)/5), which is Theta(N). T he time to Compute A mod PRIME[i] is Theta(N), and thus the time to co mpute A mod PRIME[i] for i=1..t is Theta(N^2). It can be shown that t he time for chrem is Theta(N^2), therefore the total time is Theta(N^2 )." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" } {TEXT 267 201 "Empirically verify this. Make sure that there are suff iciently many digits so that the times are sufficiently large for the \+ resolution of the Maple time function. I used 25,50,75, 100,...,250 d igits." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "Times := table();" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# >%&TimesGK%&TABLEG6#7\"Q(pprint36\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "for d from 1 to 10 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 62 " myrand := rand(1..10^(d*25)-1): # generate d*25 digit number " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " A := myrand(): B := myrand() :" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " Times[d] := time(Iprod(A,B)) :" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 22 "seq(Times[d],d=1..10);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6,$\"\"!F$$\"\"*!\"$F%F%$\"#>F'$\"#IF'$\"#RF'$\"#]F'$\"#g F'$\"#pF'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 36 "seq(evalf(Time s[d]/(25*d)),d=1..10);" }}{PARA 12 "" 1 "" {XPPMATH 20 "6,$\"\"!F$$\"+ ++++=!#8$\"+++++7F'$\"+++++!*!#9$\"++++?:F'$\"+++++?F'$\"+H9dGAF'$\"++ +++DF'$\"+nmmmEF'$\"++++gFF'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 38 "seq(evalf(Times[d]/(25*d)^2),d=1..10);" }}{PARA 12 "" 1 "" {XPPMATH 20 "6,$\"\"!F$$\"+++++O!#:$\"+++++;F'$\"+++++!*!#;$\"++++;7F' $\"+LLLL8F'$\"+)QpMF\"F'$\"++++]7F'$\"+&=&=&=\"F'$\"++++/6F'" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 269 310 "Compare to Maple's integer product. We learned previously that Maple uses Ka ratsuba for sufficiently large integers. Also, the constant for the N ^2 algorithm should be much smaller than that for our version of Iprod . Maple's code is so fast for inputs in the range that we timed, that time always returns 0." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "TimesMaple := table();" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%+TimesMapleGK%&TABLEG6#7\"Q(pprint66\"" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "for d from 1 to 10 do" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 62 " myrand := rand(1..10^(d*25)-1): # generate d*25 digit number" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " A \+ := myrand(): B := myrand():" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 29 " T imesMaple[d] := time(A*B):" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "seq(TimesMaple[d],d=1..10) ;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6,$\"\"!F$F#F#F#F#F#F#F#F#F#" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 0 "" }}}}{MARK "0 3 0" 0 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }