## What is the temporal complexity of a binary multiplication using the Karatsuba algorithm?

My apologies if the question sounds naive, but I am trying to understand the idea of ​​the complexity of time.

In general, it is said that the multiplication of Karatsuba has a temporal complexity of `O(n^1.5...)`.
The algorithm assumes that addition and subtraction take approximately `O(1)` every. However, for binary addition and subtraction, I don't think it is `O(1)`. If I'm not mistaken, a typical addition or subtraction of two binary numbers takes `O(n)` hour.

What will be the total time complexity of the next program that multiplies two binary numbers using Karatsuba Something which in turn performs binary addition and subtraction?

``````long multKaratsuba(long num1, long num2) {
if ((num1>=0 && num1<=1) && (num2>=0 && num2<=1)) {
return num1*num2;
}

int length1 = String.valueOf(num1).length(); //takes O(n)? Not sure
int length2 = String.valueOf(num2).length(); //takes O(n)? Not sure

int max = length1 > length2 ? length1 : length2;
int halfMax = max/2;

// x = xHigh + xLow
long num1High = findHigh(num1, halfMax); // takes O(1)
long num1Low = findLow(num1, halfMax); // takes O(1)

// y = yHigh + yLow
long num2High = findHigh(num2, halfMax); // takes O(1)
long num2Low = findLow(num2, halfMax); // takes O(1)

// a = (xHigh*yHigh)
long a = multKaratsuba(num1High, num2High);

// b = (xLow*yLow)
long b = multKaratsuba(num1Low, num2Low);

//c = (xHigh + xLow)*(yHigh + yLow) - (a + b);
long cX = add(xHigh,xLow); // this ideally takes O(n) time
long cY = add(yHigh,yLow); // this ideally takes O(n) time
long cXY = multKaratsuba(cX, cY);
long cAB = add(a, b) // this ideally takes O(n) time
long c = subtract(cXY, cAB) // this ideally takes O(n) time

// res = a*(10^(2*m)) + c*(10^m) + b
long resA = a * (long) Math.pow(10, (2*halfMax)); // takes O(1)
long resC = c * (long) Math.pow(10, halfMax); // takes O(1)
long resAC = add(resA, resC); // takes O(n)
long res = add(resAC, b); // takes O(n)

return res;
}
``````

## Algorithms – Karatsuba multiplication rule to divide a number into two parts

In the Karatsuba algorithm to multiply two numbers, we divide each number into two. For example:

``````x= 1234
y= 2456
``````

Then a = 12, b = 34, c = 24, d = 56

What happens if the digits in each number are not even or equal? What is the rule by dividing it into two parts?

Example:

`````` x = 12345
y = 2478
``````

or

`````` x = 12456778
y = 241
``````

## Python integer multiplication algorithm of Karatsuba

This code is not passing all the test cases, can anyone help? I just pass the direct test and then lose precision.

``````import mathematics
import unittest

IntegerMultiplier class:

def multiplies (yo, x, y):
if x <10 o and <10:
returns x * y

x = str (x)
y = str (y)

m_max = min (len (x), len (y))
x = x.rjust (m_max, & # 39; 0 & # 39;)
y = y.rjust (m_max, & # 39; 0 & # 39;)

m = math.floor (m_max / 2)

x_high = int (x[:m])
x_low = int (x[m:])

y_high = int (and[:m])
y_low = int (and[m:])

z1 = self.multiply (x_high, y_high)
z2 = self.multiply (x_low, y_low)
z3 = self.multiply ((x_low + x_high), (y_low + y_high))
z4 = z3 - z1 - z2

returns z1 * (10 ** m_max) + z4 * (10 ** m) + z2

TestIntegerMultiplier class (unittest.TestCase):

def test_normal_cases (self):
intergerMultiplier = IntegerMultiplier ()

case1 = intergerMultiplier.multiply (1234, 5678)
self.assertEqual (case1, 7006652)

yes __name__ == & # 39; __ main __ & # 39;
unittest.main ()
$$`` `$$
``````