Karatsuba multiplication for unequal dimensional, non-power operands

What is the most efficient way to implement Karatsuba multiplication of a large number with input operands of unequal size and whose size is not equal to 2 and, possibly, even an even number? Populating the operands means extra memory, and I want to try to make it memory efficient.

One of the things that I notice with an odd number of Karatsuba sizes is that if we try to divide the number into β€œhalves” as close to one as possible, half will have m + 1 element and the other m, where m = gender ( n / 2), n is the number of elements in a split number. If both numbers have the same oddness, then we need to calculate the products of two numbers of size m + 1, requiring storage of n + 1, in contrast to n in the case when n is even. Am I right in assuming that Karatsuba for weird sizes may require a bit more memory than for even sizes?

+6
source share
2 answers

In most cases, the length of the operands will not be force 2. I think this is a rare case. Most of the time, there will be different lengths of the operands. But this will not be a problem for the altar of Karatsuba.

In fact, I do not see any problem here. This overhead (odd length) is so light and certainly not big. The problem of different lengths - suppose that X = 1234 and Y = 45

So, a = 12, b = 34, c = 0, d = 45 So, after that X * Y = 10 ^ 4 * ac + 10 ^ 2 (ad + bc) + bd

 ac = 0; bd = 34 * 45; ad + bc = (a + b) * (c + d) - ac - bd = 540; 

And if we assume that we could easily multiply two-digit numbers - you could get the answer = 55530. The same as just multiply 1234 * 45 in any calculator :) So, I do not see a memory problem with different lengths of numbers.

+5
source

To answer your doubts in the comments above. The trick should follow the formula for calculating powers of 10 in the case of decimal calculation.

 10^2m(AC) + 10^m((A+B).(C+D)-AC-BD) + BD m = n/2 + n%2 n is length of number 

See the wiki for more details.

+1
source

Source: https://habr.com/ru/post/944795/


All Articles