Skip to content

Commit 00c6b72

Browse files
committed
Updated README
1 parent 94967d2 commit 00c6b72

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

Karatsuba Multiplication/README.markdown

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Goal: To quickly multiply two numbers together
44

55
## Long Multiplication
66

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!
88

9-
Example 1: Multiply 1234 by 5678 using Long Multiplication
9+
### Example 1: Multiply 1234 by 5678 using Long Multiplication
1010

1111
5678
1212
*1234
@@ -18,9 +18,9 @@ Example 1: Multiply 1234 by 5678 using Long Multiplication
1818
--------
1919
7006652
2020

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)**)
2222

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:
2424

2525
```swift
2626
// Long Multiplication
@@ -44,7 +44,7 @@ func multiply(_ num1: Int, by num2: Int, base: Int = 10) -> Int {
4444
}
4545
```
4646

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?
4848

4949
## Karatsuba Multiplication
5050

@@ -57,24 +57,17 @@ For two numbers x, y, where m <= n:
5757

5858
Now, we can say:
5959

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
6162

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:
6364

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:
71-
72-
(a+b)*(c+d) - a*c - b*c = (a*c + a*d + b*c + b*d) - a*c - b*c
73-
= (a*d + b*c)
65+
(a+b)*(c+d) - a*c - b*c = (a*c + a*d + b*c + b*d) - a*c - b*c
66+
= (a*d + b*c)
7467

7568
Pretty cool, huh?
7669

77-
Here's the full implementation
70+
Here's the full implementation. Note that the recursive algorithm is most efficient at m = n/2.
7871

7972
```swift
8073
// Karatsuba Multiplication
@@ -104,9 +97,9 @@ func karatsuba(_ num1: Int, by num2: Int) -> Int {
10497
}
10598
```
10699

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!
108101

109-
Example 2: Multiply 1234 by 5678 using Karatsuba Multiplication
102+
### Example 2: Multiply 1234 by 5678 using Karatsuba Multiplication
110103

111104
m = 2
112105
x = 1234 = a*10^2 + b = 12*10^2 + 34
@@ -126,4 +119,6 @@ Example 2: Multiply 1234 by 5678 using Karatsuba Multiplication
126119

127120
[WolframMathWorld] (http://mathworld.wolfram.com/KaratsubaMultiplication.html)
128121

122+
[Master Theorem] (https://en.wikipedia.org/wiki/Master_theorem)
123+
129124
*Written for Swift Algorithm Club by Richard Ash*

0 commit comments

Comments
 (0)