1 / 99

Grade School Revisited: How To Multiply Two Numbers

2 X 2 = 5. Grade School Revisited: How To Multiply Two Numbers. The best way is often far from obvious!. (a+bi)(c+di). Gauss. Gauss’ Complex Puzzle. Remember how to multiply two complex numbers a + bi and c + di? (a+bi)(c+di) = [ac –bd] + [ad + bc] i Input: a,b,c,d

Download Presentation

Grade School Revisited: How To Multiply Two Numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2 X 2 = 5 Grade School Revisited:How To Multiply Two Numbers

  2. The best way isoften far from obvious!

  3. (a+bi)(c+di) Gauss

  4. Gauss’ Complex Puzzle • Remember how to multiply two complex numbers a + bi and c + di? • (a+bi)(c+di) = [ac –bd] + [ad + bc] i • Input: a,b,c,d • Output: ac-bd, ad+bc • If multiplying two real numbers costs $1 and adding them costs a penny, what is the cheapest way to obtain the output from the input? Can you do better than $4.02?

  5. Gauss’ $3.05 Method (a+bi)(c+di) = [ac –bd] + [ad + bc] i Input: a,b,c,d Output: ac-bd, ad+bc • X1 = a + b • X2 = c + d • X3 = X1 X2 = ac + ad + bc + bd • X4 = ac • X5 = bd • X6 = X4 – X5 = ac - bd • X7 = X3 – X4 – X5 = bc + ad c c $ $ $ c cc

  6. The Gauss optimization saves one multiplication out of four. It requires 25% less work.

  7. * * * ** * * ** * * ** * * ** * * ** * * ** * * ** * * ** * * ** * * ** * ** * + We saw that T(n) was linear. Time complexity of grade school addition T(n) = The amount of time grade school addition uses to add two n-bit numbers T(n) = Θ(n)

  8. X * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * n2 * * * * * * * * We saw that T(n) was quadratic. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Time complexity of grade school multiplication T(n) = The amount of time grade school multiplication uses to add two n-bit numbers T(n) = Θ(n2)

  9. Grade School Addition: Linear timeGrade School Multiplication: Quadratic time • No matter how dramatic the difference in the constants the quadratic curve will eventually dominate the linear curve time # of bits in numbers

  10. Grade school addition is linear time. Is there a sub-linear time method for addition?

  11. Any addition algorithm takes Ω(n) time Claim: Any algorithm for addition must read all of the input bits Proof: Suppose there is a mystery algorithm A that does not examine each bit Give A a pair of numbers. There must be some unexamined bit position i in one of the numbers

  12. Any addition algorithm takes Ω(n) time • If A is not correct on the inputs, we found a bug • If A is correct, flip the bit at position i and give A the new pair of numbers. A gives the same answer as before, which is now wrong. * * * * * * * * * A did not read this bit at position i * * * * * * * * * * * * * * * * * * *

  13. So any algorithm for addition must use time at least linear in the size of the numbers. Grade school addition can’t be improved upon by more than a constant factor.

  14. Grade School Addition: Θ(n) timeFurthermore, it is optimalGrade School Multiplication: Θ(n2) time Is there a clever algorithm to multiply two numbers in linear time?

  15. Despite years of research, no one knows! If you resolve this question, Carnegie Mellon will give you a PhD!

  16. Can we even break the quadratic time barrier? In other words, can we do something very different than grade school multiplication?

  17. Grade School Multiplication:The Kissing Intuition Intuition: Let’s say that each time an algorithm has to multiply a digit from one number with a digit from the other number, we call that a “kiss”. It seems as if any correct algorithm must kiss at least n2 times.

  18. Divide And Conquer • An approach to faster algorithms: • DIVIDE a problem into smaller subproblems • CONQUER them recursively • GLUE the answers together so as to obtain the answer to the larger problem

  19. Multiplication of 2 n-bit numbers n bits • X = • Y = X a b Y c d n/2 bits n/2 bits X = a 2n/2 + b Y = c 2n/2 + d X × Y = ac 2n + (ad + bc) 2n/2 + bd

  20. Multiplication of 2 n-bit numbers a b • X = • Y = c d n/2 bits n/2 bits X × Y = ac 2n + (ad + bc) 2n/2 + bd MULT(X,Y): If |X| = |Y| = 1 then return XY break X into a;b and Y into c;d return MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)

  21. Same thing for numbers in decimal! n digits • X = • Y = a b c d n/2 digits n/2 digits X = a 10n/2 + b Y = c 10n/2 + d X × Y = ac 10n + (ad + bc) 10n/2 + bd

  22. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • *4276 5678*2139 5678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  23. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  24. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*42765678*2139 5678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  25. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*21395678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  26. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  27. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  28. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*2112*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  29. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  30. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  31. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  32. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  33. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • 1*2 1*1 2*2 2*1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  34. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*2112*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx • Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 252 2 1 4 2 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  35. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 25212*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx • Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 252 2 1 4 2 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  36. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 25212*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx • Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 252 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  37. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1*3 1*9 2*3 2*9 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  38. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 242 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3 9 6 18 *102 + *101 + *101 + *1 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  39. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 242 468 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3 9 6 18 *102 + *101 + *101 + *1 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  40. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 252 468 714 1326 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  41. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 252 468 714 1326 *104 + *102 + *102 + *1 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  42. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 1234*2139 1234*4276 5678*2139 5678*4276 • 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx • = 2639526 252 468 714 1326 *104 + *102 + *102 + *1 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  43. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 2639526 1234*4276 5678*2139 5678*4276 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  44. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 2639526 5276584 12145242 24279128 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  45. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 2639526 5276584 12145242 24279128 *108 + *104 + *104 + *1 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  46. a b c d Multiplying (Divide & Conquer style) • 12345678 * 21394276 • 2639526 5276584 12145242 24279128 *108 + *104 + *104 + *1 = 264126842539128 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  47. a b c d Multiplying (Divide & Conquer style) 264126842539128 • X = • Y = • X × Y = ac 10n + (ad + bc) 10n/2 + bd

  48. Divide, Conquer, and Glue • MULT(X,Y)

  49. Divide, Conquer, and Glue if |X| = |Y| = 1 then return XY, else… • MULT(X,Y):

  50. Divide, Conquer, and Glue X=a;b Y=c;d • MULT(X,Y):

More Related