Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bad380e

Browse files
committedOct 16, 2018
finish README
1 parent e1f7d7e commit bad380e

File tree

1 file changed

+35
-1
lines changed
  • src/algorithms/math/karatsuba-multiplication

1 file changed

+35
-1
lines changed
 

‎src/algorithms/math/karatsuba-multiplication/README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@
22

33
Karatsuba is a fast multiplication algorithm discovered by Anatoly Karatsuba in 1960. Given two n-digit numbers, the "grade-school" method of long multiplication has a time complexity of O(n<sup>2</sup>), whereas the karatsuba algorithm has a time complexity of O(n<sup>1.59</sup>).
44

5+
## Recursive Formula
6+
7+
```
8+
x = 1234
9+
y = 5678
10+
11+
karatsuba(x, y)
12+
```
13+
14+
1. Split each number into numbers with half as many digits
15+
```
16+
a = 12
17+
b = 34
18+
19+
c = 56
20+
d = 78
21+
```
22+
23+
2. Compute 3 subexpressions from the smaller numbers
24+
- `ac = a * c`
25+
- `bd = b * d`
26+
- `abcd = (a + b) * (c + d)`
27+
28+
3. Combine subexpressions to calculate the product
29+
```
30+
A = ac * 1000
31+
B = (abcd - ac - bd) * 100
32+
C = bd
33+
34+
x * y = A + B + C
35+
```
36+
37+
_**Note:**_ *The karatsuba algorithm can be applied recursively to calculate each product in the subexpressions.* (`a * c = karatsuba(a, c)`*). When the numbers get smaller than some arbitrary threshold, they are multiplied in the traditional way.*
38+
539
## References
640
[Stanford Algorithms (YouTube)](https://www.youtube.com/watch?v=JCbZayFr9RE)
7-
[Wikipedia](https://en.wikipedia.org/wiki/Karatsuba_algorithm)
41+
[Wikipedia](https://en.wikipedia.org/wiki/Karatsuba_algorithm)

0 commit comments

Comments
 (0)
Please sign in to comment.