简体   繁体   中英

Karatsuba algorithm with O(n) memory instead of O(n log n)

The recursive Karatsuba multiplication algorithm with time complexity ~O(n^log2(3)) is known to be faster than the simple multiplication algorithm that have the complexity O(n^2) .
But, it uses much more memory space than the "schoolgrade" algorithm - O(n log(n)) instead of O(n) memory, and that's really critical for controllers or very small computers that has a small amount of memory.

So, my question is:
Is there a way to achieve an O(n) memory space in the karatsuba algorithm?

PS: I know that with the FFT you can achieve much faster speed O(n log(n)) and much better memory usage O(n) , but i'm just curious about the Karatsuba.

Karatsuba's algorithm only requires O(n) space.

(A0 2^n + B0)(A1 2^n + B1)
= A0 A1 2^(2n) + B0 B1 + ((A0+B0)(A1+B1) - A0 A1 - B0 B1)2^n

Here is a rough inductive argument. Inductively, suppose multiplying n-digit numbers using Karatsuba's algorithm takes only cn space. To multiply 2n-digit numbers, we can multiply A0 A1 in cn space, then save the answer in 2n space , then multiply B0 B1 in cn space, then save the answer in 2n space, then multiply (A0+B0)(A1+B1) in cn space. At this point we are using at most (c+4)n space. Then we perform the subtractions and record the answer in 4n space. The peak space usage was (c+4)n which is less than max(c,4)(2n) space. So, as long as c>4, if it takes cn space to multiply n digit numbers, then it takes c(2n) space to multiply 2n digit numbers. This was imprecise because (A0+B0) and (A1+B1) may have n+1 digits instead of n. So, a rigorous inductive argument is more messy, but it can be done following the same basic pattern.

The premise of the question is wrong. Karatsuba multiplication only requires O(n) space, not Omega(n log n). It is possible that some implementations would require more space not necessarily limited to O(n log n), such as if you do the calculations in parallel.

In fact, it is possible to do Karatsuba multiplication in O(log n) extra bits beyond the answer.

The answer is yes:

It is possible to perform the "Karatsuba" algorithm in-place, saving the additional memory per recursion, using N entries for the input arrays and 2*N entries for the output array.

Have a look here for an example: https://github.com/hselasky/libmbin/blob/master/mbin_multiply_x3.c

--HPS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM