How to compare various multiplication algorithms over a range of numbers

320 views Asked by At

While going through a MIT lecture in MITOpencourseware (6.006 lecture 12), I came across the mention of 4 multiplication algorithms (to multiply two n-digit numbers) -

  1. Ordinary naive approach with O(n^2) complexity
  2. Karatsuba algorithm - O(n^1.584)
  3. Toom-Cook(Toom3) - O(n^1.465)
  4. Schonhage-Strassen - O(nlog(n)log(log(n)))

Now the thing to investigate was at which threshold points (i.e., values of n) did one method overtake the other as a better algorithm. It was mentioned that all the above are in gmpy package.

In order to try this out I referred to the gmpy2 package documentation at the following link - https://gmpy2.readthedocs.io/en/latest/intro.html

However on skimming through portions of this documentation it seemed that gmpy2 is more about dealing with large numbers. In particular I did not find separate functions implementing each of these above 4 algorithms. So are there any portions of gmpy2 where these algorithms are implemented so I may plot the running times of these algorithms against n (num of digits)?

1

There are 1 answers

0
casevh On BEST ANSWER

I'm the maintainer of gmpy2.

gmpy2 doesn't directly implement any multiplication algorithms. It just calls the algorithms implemented in GMP

Many years ago, I wrote a pure Python implementation of multiple-precision arithmetic. It uses naive, Karatsuba, Toom-3, Toom-4, and Nussbaumer convolution for multiplication. (Interestingly, if you choose a sufficiently large precision, a pure Python solution that recursively calls Toom-4, Toom-3, Karatsuba can be faster than the Karatsuba only version used in Python and written in C.) A couple of different division algorithms are also implemented.

It is easy to modify the code and add global variable to count the number of times each step is executed.

It can be found at DecInt