Number Theory Algorithms Zeph Grunschlag Copyright Zeph Grunschlag, 2 22.
Agenda Euclidean Algorithm for GCD Number Systems Decimal numbers (base ) Binary numbers (base 2) One s complement Two s complement General base b number systems Arithmetic Algorithms Addition Multiplication Subtraction s and 2 s complement L 2
Euclidean Algorithm m, n Euclidean Algorithm gcd(m,n) integer euclid(pos. integer m, pos. integer n) = m, y = n while(y > ) r = mod y = y y = r return L 3
Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 L 4
Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 33 mod 77 = 33 77 33 L 5
Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 33 mod 77 = 33 77 33 2 77 mod 33 = 33 L 6
Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 2 3 33 mod 77 = 33 77 mod 33 = 33 mod = 77 33 33 L 7
Euclidean Algorithm. Eample gcd(244,7): Step r = mod y y - 244 7 L 8
Euclidean Algorithm. Eample gcd(244,7): Step r = mod y y - 244 7 244 mod 7 = 7 L 9
Euclidean Algorithm. Eample gcd(244,7): Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 L
gcd(244,7): Euclidean Algorithm. Eample Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 3 mod 7 = 3 7 3 L
gcd(244,7): Euclidean Algorithm. Eample Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 3 mod 7 = 3 7 3 4 7 mod 3 = 3 L 2
gcd(244,7): Euclidean Algorithm. Eample Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 3 mod 7 = 3 7 3 4 7 mod 3 = 3 5 3 mod = By definition 244 and 7 are rel. prime. L 3
Euclidean Algorithm Correctness The reason that Euclidean algorithm works is gcd(,y ) is not changed from line to line. If, y denote the net values of, y then: gcd(,y ) = gcd(y, mod y) = gcd(y, qy) (the useful fact) = gcd(y, ) (subtract y multiple) = gcd(,y) L 4
Euclidean Algorithm Running Time EX: Compute the asymptotic running time of the Euclidean algorithm in terms of the number of mod operations: L 5
Euclidean Algorithm Running Time Assuming mod operation is O (): integer euclid(m, n) = m, y = n while( y > ) r = mod y = y y = r return O () +? ( O () + O () + O () + O () ) + O () =? O() Where? is the number of while loop iterations. L 6
Euclidean Algorithm Running Time Facts: ( = net value of, etc. ) 2. can only be less than y at very beginning of algorithm once > y, = y > y = mod y 4. When > y, two iterations of while loop guarantee that new is < ½ original because = y = mod y. Two cases: I. y > ½ mod y = y < ½ II. y ½ mod y < y ½ L 7
Euclidean Algorithm Running Time (&2) After first iteration, size of decreases by factor > 2 every two iterations. I.e. after 2m+ iterations, < original_ / 2 m Q: When in terms of m does this process terminate? L 8
Euclidean Algorithm Running Time After 2m+ steps, < original_ / 2 m A: While loop eits when y is, which is right before would have gotten =. Eiting while loop happens when 2 m > original_, so definitely by: m = log 2 ( original_ ) Therefore running time of algorithm is: O(2m+) = O(m) = O (log 2 (ma(a,b)) ) L 9
Euclidean Algorithm Running Time Measuring input size in terms of n = number of digits of ma(a,b): n = Θ(log (ma(a,b)) ) = Θ(log 2 (ma(a,b)) ) Therefore running time of algorithm is: O(log 2 (ma(a,b)) ) = O(n) (assumed naively that mod is an O() operation, so estimate only holds for fied size integers such as int s and long s) L 2
Number Systems Q: What does the string of symbols 234 really mean as a number and why? L 2
Number Systems A: 2 thousands hundreds 3 tens and 4 = 2 3 + 2 + 3 + 4 But on the planet Ogg, the intelligent life forms have only one arm with 5 fingers. L 22
Number Systems So on Ogg, numbers are counted base 5. I.e. on Ogg 234 means: 2 5 3 + 5 2 + 3 5 + 4 5 To distinguish between these systems, subscripts are used: (234) for Earth (234) 5 for Ogg L 23
Number Systems DEF: A base b number is a string of symbols u = a k a k a k 2 a 2 a a With the a i in {,,2,3,,b 2,b }. The string u represents the number (u ) b = a k b k + a k b k +... + a b + a NOTE: When b >, run out of decimal number symbols so after 7, 8, 9 use capital letters, starting from A, B, C, L 24
Number Systems EG: base 2 (binary), base 8 (octal ) 74, 472 base 6 (headecimal ) 2F, ABCD Q: Compute the base version of these. L 25
Number Systems A: base 2 (binary), () 2 = 2 2 + 2 + 2 = 5 () 2 = (2 4 +2 3 +2 2 +2 ) + 2 = 2 base 8 (octal ) 74, 472 (74) 8 = 7 8 + 4 8 = 6 (472) 8 = 4 8 2 + 7 8 + 2 8 = 34 base 6 (headecimal ) 2F, ABCD (2F) 6 = 6 2 +2 6 +5 6 = 33 (ABCD) 6 = 6 3 + 6 2 +2 6 +3 6 L 26
Number Systems Binary most natural system for bit strings and headecimal compactifies byte strings ( byte = 2 headecimals) EG in HTML: <font color="ffff"> Nice Color </font> Q: What color will this become? L 27
Number Systems A: "ffff" represents the rgb value: The first byte is for redness, the second byte is for green ness, and the last for blue ness. The HTML above specifies that 5 6 + 5 = 255 redness and blueness values, but 6 + = green ness. Red and blue give purple, and 255 is the top brightness so this is bright purple. L 28
Number Systems Reverse Conversion Convert arbitrary decimal numbers into various bases, (calculator functions typically limited to base 2, 8, 6 and ). EG: Back at Ogg. Convert 646 to base 5. Try to do all operations base 5. L 29
Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = (6) () 2 + (4) () + (6) Each quantity easy to convert into base 5: (6) =() 5 since 6 = 5 + (4) =(4) 5 since 4 < 5 () =(2) 5 since = 2 5 + So convert whole epression and do Oggian arithmetic: L 3
Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = L 3
Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = () 5 (4) 5 + (3) 5 + () 5 = L 32
Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = () 5 (4) 5 + (3) 5 + () 5 = (44) 5 + (4) 5 = L 33
Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = () 5 (4) 5 + (3) 5 + () 5 = (44) 5 + (4) 5 = (4) 5 Thinking like an Oggian hurts brain too much L 34
Number Systems Reverse Conversion Given an integer n and a base b find the string u such that (u ) b = n. Pseudocode: string represent(pos. integer n, pos. integer b) q = n, i = while( q > ) u i = q mod b q = q/b i = i + return u i u i u i 2 u 2 u u L 35
Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - q = q/b 646 L 36
Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = q = q/b 646 646/5 =29 L 37
Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = 29 mod 5 = 4 q = q/b 646 646/5 =29 29/5 =25 L 38
Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = 29 mod 5 = 4 2 25 mod 5 = q = q/b 646 646/5 =29 29/5 =25 25/5 =5 L 39
Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = 29 mod 5 = 4 2 25 mod 5 = 3 5 mod 5 = q = q/b 646 646/5 =29 29/5 =25 25/5 =5 5/5 = L 4
Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i 4 u i = q mod b - 646 mod 5 = 29 mod 5 = 4 2 25 mod 5 = 3 5 mod 5 = mod 5 = Reading last column in reverse: 4 q = q/b 646 646/5 =29 29/5 =25 25/5 =5 5/5 = /5 = L 4
Number Systems In Class Eercise Some number theory facts are base dependent. For eample First Grade Teacher s Rule: A base number is divisible by 3 iff the sum of its digits are. Formally, let n = (u k u k u k 2 u 2 u u ). Then: n mod 3= u i mod 3 i= EG: 3 235 because 3 (+2++3+5 = 2) k L 42
Arithmetical Algorithms Let s write down some familiar arithmetical algorithms. Conveniently, they run the same in any number base. In some cases, such as addition, there are asymptotically faster approaches, but these are the simplest procedures and tend to be fastest for relatively small (e.g. < bits) number sizes. L 43
Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: base base 6 Carry: 7 4 6 3 A 4 F + y + 2 9 9 + C B 9 L 44
Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y base base 6 7 4 6 3 A 4 F + 2 9 9 + C B 9 2 L 45 9
Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y base base 6 7 4 6 3 A 4 F + 2 9 9 + C B 9 7 2 L 46 F 9
Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y base base 6 7 4 6 3 A 4 F + 2 9 9 + C B 9 3 7 2 L 47 F F 9
Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y 7 4 6 3 A 4 F + 2 9 9 + C B 9 base base 6 3 7 2 L 48 6 F F 9
Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y 7 4 6 3 A 4 F + 2 9 9 + C B 9 base base 6 3 7 2 L 49 6 F F 9
Arithmetical Algorithms Addition of Positive Numbers string add(strings k k, y k y k y y, int base) carry =, k+ = y k+ = for(i = to k+) digitsum = carry + i + y i z i = digitsum mod base carry = digitsum /base return z k+ z k z k z z L 5
s Complement 2 s Complement The binary number system makes some operations especially simple and efficient under certain representations. Two such representations are s complement 2 s complement Each makes subtraction much simpler. Each has disadvantage that number length is predetermined. L 5
s Complement Fi k bits. (EG, k = 8 for bytes) Represent numbers with < 2 k Left most bit tells the sign positive (so positive no. s as usual) negative (but other bits change too!) Positive numbers the same as standard binary epansion Negative numbers gotten by taking the boolean complement, hence nomenclature L 52
s Complement Eamples k = 8: represents 8 represents 8 Notice: when add these representations as usual get, i.e. negative or =. Guess: adding numbers with mied sign works the same as adding positive numbers Trade off: not unique L 53
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 54
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 55
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 56
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 57
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 58
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 59
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 6
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 6
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 62
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 63
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 64
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 65
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 66
s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 67
2 s Complement Fies the non uniqueness of zero problem Adding mied signs still easy No cycle overflow (pre computed) Java s approach (under the hood) Same fied length k, sign convention, and definition of positive numbers as with s complement Represent numbers with 2 (k ) < 2 (k ) EG. Java s byte ranges from 28 to +27 L 68
2 s Complement Negatives (slightly harder than s comp.): Compute s complement Add Summarize: = +. represents 8 + = represents 8. Add together without over flow: Q: What are the ranges of Java s 32 bit int and 64 bit long? (All of Java s integer types use 2 s complement) L 69
2 s Complement A: 2) 32 bit int s: Largest int =. = 2 3 = 2,47,483,647 Smallest int =. = 2 3 = 2,47,483,648 2) 64 bit long s: Largest long =. = 2 63 = 9,223,372,36,854,775,87 Smallest int =. = 2 63 = 9,223,372,36,854,775,88 L 7
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 7
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 72
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 73
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 74
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 75
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 76
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 77
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 78
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 79
2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y As a final check take the negative to see if get 3: ( +) = (+) =. YES! L 8
Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 8
Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 82
Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 83
Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 84
Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 85
Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 86
Arithmetical Algorithms Binary Multiplication bitstring multiply(bitstrings k k, y k y k y y ) = k k p = // the partial product for(i = to k+) if(y i == ) return p p = add(p, << i ) // prev. algorithm L 87