Presentation is loading. Please wait.

Presentation is loading. Please wait.

How To Multiply Two Numbers

Similar presentations


Presentation on theme: "How To Multiply Two Numbers"— Presentation transcript:

1 How To Multiply Two Numbers
12 X 22 = 264

2 The best way is often far from obvious!

3 Gauss’ Complex Puzzle Remember how to multiply 2 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 a real multiplication costs $1 and an addition cost a penny. What is the cheapest way to obtain the output from the input? Can you do better than $4.02?

4 Gauss’ $3.05 Method: Input: a,b,c,d Output: ac-bd, bc+ad
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

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

6 Time complexity of grade school addition
* * * * * * + T(n) = The amount of time grade school addition uses to add two n-bit numbers T(n) for addition is linear

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

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

9 Is there a clever algorithm to multiply two numbers in linear time?
Grade School Addition: O(n) time Furthermore, it is optimal Grade School Multiplication: O(n2) time Is there a clever algorithm to multiply two numbers in linear time?

10 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

11 Multiplication of 2 n-bit numbers
X = Y = X = a 2n/2 + b Y = c 2n/2 + d XY = ac 2n + (ad+bc) 2n/2 + bd a b c d

12 Multiplication of 2 n-bit numbers
X = Y = XY = ac 2n + (ad+bc) 2n/2 + bd a b c d 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)

13 Multiplying (Divide & Conquer style)
* * * *4276

14 Multiplying (Divide & Conquer style)
* 1234* * * *4276

15 Multiplying (Divide & Conquer style)
* 1234* * * *4276

16 Multiplying (Divide & Conquer style)
* 1234* * * *4276

17 Multiplying (Divide & Conquer style)
* 1234* * * *4276

18 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx

19 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx

20 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

21 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

22 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2* (1 + 4) = 252

23 Multiplying (Divide & Conquer style)
* 1234* * * *4276 * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2* (1 + 4) = 252

24 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx

25 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx * * * *1

26 Multiplying (Divide & Conquer style)
* 1234* * * *4276 12* * * *39 xxxxxxxxxxxxxxxxxxxxxxxxx = * * * *1

27 Multiplying (Divide & Conquer style)
* * * *4276

28 Multiplying (Divide & Conquer style)
*

29 Multiplying (Divide & Conquer style)
* * * * *1

30 Multiplying (Divide & Conquer style)
* * * * *1 =

31 Divide, Conquer, and Glue
MULT(X,Y)

32 Divide, Conquer, and Glue
If |X|=|Y|=1, Then Return XY Else …. MULT(X,Y):

33 Divide, Conquer, and Glue
X=a;b Y=c;d MULT(X,Y): Mult(a,c) Mult(b,d) Mult(a,d) Mult(b,c)

34 Divide, Conquer, and Glue
X=a;b Y=c;d MULT(X,Y): Mult(b,d) Mult(a,d) Mult(b,c) Mult(a,c)

35 Divide, Conquer, and Glue
X=a;b Y=c;d ac MULT(X,Y): ac Mult(b,d) Mult(a,d) Mult(b,c)

36 Divide, Conquer, and Glue
X=a;b Y=c;d ac MULT(X,Y): ac Mult(b,d) Mult(b,c) Mult(a,d)

37 Divide, Conquer, and Glue
X=a;b Y=c;d ac, ad MULT(X,Y): ac Mult(b,d) ad Mult(b,c)

38 Divide, Conquer, and Glue
X=a;b Y=c;d ac, ad, bc, bd MULT(X,Y): ac bd ad bc

39 Divide, Conquer, and Glue
X=a;b Y=c;d ac2n + (ad+bc)2n/2 + bd MULT(X,Y): ac bd ad bc

40 Time required by MULT T(n) = time taken by MULT on two n-bit numbers
What is T(n)? What is its growth rate? Is it O(n2)?

41 T(n) = 4 T(n/2) + k’ n + k’’ , n=|X|
Conquer Divide and glue X=a;b Y=c;d ac2n + (ad+bc)2n/2 + bd Time: k’n + k” ac bd ad bc T(n/2) T(n/2) T(n/2) T(n/2)

42 Recurrence Relation T(1) = k for some constant k
T(n) = 4 T(n/2) + k’ n + k’’ for some constants k’ and k’’ 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)

43 Multiplication Algorithms
Kindergarten n2n Grade School n2 Karatsuba(Divide and Conquer with Gauss) n1.58… Fastest Known n logn loglogn

44 Recursion Example fun print_base(int num, int base)
if (num/base) > 0 print_base(num/base, base); disp(mod(num,base)); end

45 Recursion Example expression = (expression)
expression = expression * expression expression = expression / expression expression = expression + expression expression = expression – expression expression = variable expression = constant

46 Crossword Puzzle List of Words:
1 2 3 4 5 6 7 8 List of Words: AFT LASER ALE LEE EEL LINE HEEL SAILS HIKE SHEET HOSES STEER KEEL TIE KNOT Goal: Find words to fill the puzzle such that a new word starts at every numbered cell

47 Crossword Puzzle Solution: How do we solve problems Of this type?
Another Example: Find Solution for: S E N D + M O R E M O N E Y 1 H O 2 S E 3 A T 4 I 5 K 6 7 L 8 R

48 Representation of the Problem
1 H O 2 S E 3 A T 4 I 5 K 6 7 L 8 R 4A 1A 2D 3D 8A 7A 5D Graph of “Constraints” 6D

49 Crossword Puzzle Constraints help us reduce the domains For variables.
1 H O 2 S E 3 A T 4 I 5 K 6 7 L 8 R Crossword Puzzle Constraints help us reduce the domains For variables. Variable Starting Cell Domain 1Across 1(5) HOSES, LASER, SAILS, SHEET, STEER 4Across 4(4) HEEL, HIKE, KEEL, KNOT, LINE 7Across 7(3) AFT, ALE, EEL, LEE, TIE 8Across 8(5) 2Down 2(5) 3Down 3(5) 5Down 6Down 6(3)

50 Constraints 1A 2D 3D 7A 5D 4A 8A 6D 1ACROSS[3] = 2DOWN[1] 1ACROSS[5] = 3DOWN[1] 4ACROSS[2] = 2DOWN[3] 4ACROSS[3] = 5DOWN[1] 4ACROSS[4] = 3DOWN[3] 7ACROSS[1] = 2DOWN[4] 7ACROSS[2] = 5DOWN[2] 7ACROSS[3] = 3DOWN[4] 8ACROSS[1] = 6DOWN[2] 8ACROSS[3] = 2DOWN[5] 8ACROSS[4] = 5DOWN[3] 8ACROSS[5] = 3DOWN[5]

51 (xi,xj) NEW Di, if changed ADDED {xk,xi} (1A 2D) HOSES,LASER (3D 1A)
SAILS, SHEETS, STEER (4A,3D) (7A,3D) (8A,3D) (4A,3D) HEEL,HIKE,KEEL (2D,4A) (5D,4A) (7A,3D) ALE,EEL,LEE,TIE (2D,7A) (5D,7A) (8A,3D) (2D,4A) SAILS,SHEET,STEER (1A,2D) (7A,2D) (8A,2D) (5D,4A) KEEL,KNOT (7A,5D) (8A,5D) (2D,7A) (5D,7A) KEEL (4A,5D) (1A,2D) (7A,2D) EEL,LEE (3D,7A) (5D,7A) (8A,2D) (3D,8A) (5D,8A) (6D,8A) (7A,5D) (8A,5D)

52 (xi,xj) NEW Di, if changed ADDED {xk,xi} (4A,5D) HIKE (2D,4A) (3D,4A)
SAILS,STEER (1A,3D) (4A,3D) (7A,3D) (5D,8A) (6D,8A) ALE (2D,4A) SAILS (1A,2D) (7A,2D) (8A,2D) (3D,4A) STEER (8A,3D) (1A,3D) HOSES (2D,1A) (4A,3D) (7A,3D) LEE (2D,7A) (5D,7A) Etc.


Download ppt "How To Multiply Two Numbers"

Similar presentations


Ads by Google