Created
April 11, 2019 16:19
-
-
Save DiegoGallegos4/2a945d0515dfe7347ce1052bb6605643 to your computer and use it in GitHub Desktop.
Karatsuba Multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def karatsuba(A, B, n): | |
""" | |
A(x) = a_1 * x + a_0 | |
B(x) = b_1 * x + b_0 | |
C(x) = a_1 * b_1 * x^2 + (a_1 * b_0 + a_0 * b_1) * x + a_0 * b_0 | |
Rewrite as | |
C(x) = a_1 * b_1 * x^2 + ((a_1 + a_0) * (b_0 + b_1) - a_1 * b_1 - a_0 * b_0) * x + a_0 * b_0 | |
Reduce the problem to 3 multiplications | |
Running time: | |
O(3^log2n) -> O(n^log2(3)) = O(n^1.58) | |
Master Theorem: | |
if T(n) = a * T(ceil(n/b)) + O(n^d) | |
constants a > 0,b > 1,d >= 0 then | |
if d > logb(a) then O(n^d) | |
if d = logb(a) then O(n^d * logn) | |
if d < logb(a) then O(n^logb(a)) | |
O(n^d) amount of work on each level from 1 to log2(n) | |
""" | |
product = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment