You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Karatsuba Multiplication/README.markdown
+15-20Lines changed: 15 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ Goal: To quickly multiply two numbers together
4
4
5
5
## Long Multiplication
6
6
7
-
In grade school we all learned how to multiply two numbers together via Long Multiplication. So let's try that first!
7
+
In grade school we learned how to multiply two numbers together via Long Multiplication. Let's try that first!
8
8
9
-
Example 1: Multiply 1234 by 5678 using Long Multiplication
9
+
### Example 1: Multiply 1234 by 5678 using Long Multiplication
10
10
11
11
5678
12
12
*1234
@@ -18,9 +18,9 @@ Example 1: Multiply 1234 by 5678 using Long Multiplication
18
18
--------
19
19
7006652
20
20
21
-
So what's the problem with long multiplication? Speed. Long Multiplication runs in **O(n^2)**.
21
+
So what's the problem with Long Multiplication? Well remember the first part of our goal. To *quickly* multiply two numbers together. Long Multiplication is slow! (**O(n^2)**)
22
22
23
-
You can see where the **O(n^2)** comes from in the implementation for Long Multiplication:
23
+
You can see where the **O(n^2)** comes from in the implementation of Long Multiplication:
24
24
25
25
```swift
26
26
// Long Multiplication
@@ -44,7 +44,7 @@ func multiply(_ num1: Int, by num2: Int, base: Int = 10) -> Int {
44
44
}
45
45
```
46
46
47
-
The double for loop is the culprit! So Long Multiplication might not be the best algorithm after all. Can we do better?
47
+
The double for loop is the culprit! By comparing each of the digits (as is necessary!) we set ourselves up for an **O(n^2)** running time. So Long Multiplication might not be the best algorithm after all. Can we do better?
48
48
49
49
## Karatsuba Multiplication
50
50
@@ -57,24 +57,17 @@ For two numbers x, y, where m <= n:
57
57
58
58
Now, we can say:
59
59
60
-
x*y = a*c*10^(2m) + (a*d + b*c)*10^(m) + b*d
60
+
x*y = (a*10^m + b) * (c*10^m + d)
61
+
= a*c*10^(2m) + (a*d + b*c)*10^(m) + b*d
61
62
62
-
We can compute this function recursively, and that's what makes Karatsuba Multiplication fast.
63
+
This had been know since the 19th century. The problem is that the method requires 4 multiplications (`a*c`, `a*d`, `b*c`, `b*d`). Karatsuba's insight was that you only need three! (`a*c`, `b*d`, `(a+b)*(c+d)`). Now a perfectly valid question right now would be "How is that possible!?!" Here's the math:
63
64
64
-
```swift
65
-
let ac =karatsuba(a, by: c)
66
-
let bd =karatsuba(b, by: d)
67
-
let adPlusbc =karatsuba(a+b, by: c+d) - ac - bd
68
-
```
69
-
70
-
The last recursion is interesting. Normally, you'd think we would have to run four recursions to find the product `x*y` (`a*c`, `a*d`, `b*c`, `b*d`). However, Karatsuba realized that you only need three recursions, and some addition and subtraction. Here's the math:
Here's the full implementation. Note that the recursive algorithm is most efficient at m = n/2.
78
71
79
72
```swift
80
73
// Karatsuba Multiplication
@@ -104,9 +97,9 @@ func karatsuba(_ num1: Int, by num2: Int) -> Int {
104
97
}
105
98
```
106
99
107
-
The run time for this algorithm is about **O(n^1.56)** which is better than the **O(n^2)** for Long Multiplication.
100
+
What about the running time of this algorithm? Is all this extra work worth it? We can use the Master Theorem to answer this question. This leads us to `T(n) = 3*T(n/2) + c*n + d` where c & d are some constants. It follows (because 3 > 2^1) that the running time is **O(n^log2(3))** which is roughly **O(n^1.56)**. Much better!
108
101
109
-
Example 2: Multiply 1234 by 5678 using Karatsuba Multiplication
102
+
### Example 2: Multiply 1234 by 5678 using Karatsuba Multiplication
110
103
111
104
m = 2
112
105
x = 1234 = a*10^2 + b = 12*10^2 + 34
@@ -126,4 +119,6 @@ Example 2: Multiply 1234 by 5678 using Karatsuba Multiplication
0 commit comments