Integer Multiplication Flashcards
Different Integer Multiplication Algorithms
Regular
Divide Conquer
Karatsuba Algo
Regular Algorithm For Integer Multiplication Input
Input will be two numbers in the form of an array each value corresponding to its 10th power, the Same as output
This reduces operations by multiplying by single digits and shifting resulting in the possibility of around n operations.
Time Complexity and Space Complexity of Regular Algorithm For Integer Mul
Time Complex ; Theta(n^2)
Space; Theta(n)
For a long time, scientists thought it was the fastest.
Regular Algorithm For Integer Multiplication : General
Grade School Multiplication
Divide Conquer Integer Multiplication Input
Input: X, Y, n
Let X,Y be n-digit numbers
Divide Conquer Integer Multiplication Time Complexity
Omega(n^2)
Not any better than the regular algorithm, but it is the intermediate between the fastest, Karatsuba where we use a portion of this algorithm
Divide Conquer Integer Multiplication Algorithm
Function SplitMul(x, y, n)
1: if n = 1 then
2: return x · y
3: else
4: m ← ⌈n/2⌉
5: a ← ⌊x/10m⌋; b ← x mod 10m
7: c ← ⌊y/10m⌋; d ← y mod 10m
8: e ← SplitMul(a, c, m)
9: f ← SplitMul(b, d, m)
10: g ← SplitMul(b, c, m)
11: h ← SplitMul(a, d, m)
12: return 10^(2m) e + 10^m *(g + h) + f
REASON:
■ Denote m = ⌈n/2⌉,
■ Let x = (10^m · a + b) and y = (10^m · c + d),
■ Then, xy = 10^(2m)ac + 10^m(bc + ad) + bd.
Karatsuba Algorithm Input
Input: X, Y, n
Let X,Y be n-digit numbers
Karatsuba Time Complexity
FASTEST:
O(n^1.58)
T(n) = 3T(⌈n/2⌉) + Θ(n)
Because 1 less recursion call
Karatsuba Algorithm
From Divide and Conquer we know we need xy = 10^(2m)ac + 10^m(bc + ad) + bd. To get bc + ad we do ac+bd −(a−b)(c−d) = bc+ad.
HERE IS ALGO:
Function FastMul(x, y, n)
1: if n = 1 then
2: return x · y
3: else
4: m ← ⌈n/2⌉
5: a ← ⌊x/10m⌋; b ← x mod 10m
7: c ← ⌊y/10m⌋; d ← y mod 10m
8: e ← SplitMul(a, c, m)
9: f ← SplitMul(b, d, m)
10: g ← SplitMul(a-b, c-d, m)
12: return 10^(2m) e + 10^m *(e+f-g) + f