Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which algorithm to choose for a huge integer multiplication, depending on N size

Tags:

algorithm

In my free time I'm preparing for interview questions like: implement multiplying numbers represented as arrays of digits. Obviously I'm forced to write it from the scratch in a language like Python or Java, so an answer like "use GMP" is not acceptable (as mentioned here: Understanding Schönhage-Strassen algorithm (huge integer multiplication)).

For which exactly range of sizes of those 2 numbers (i.e. number of digits), I should choose

  1. School grade algorithm
  2. Karatsuba algorithm
  3. Toom-Cook
  4. Schönhage–Strassen algorithm ?

Is Schönhage–Strassen O(n log n log log n) always a good solution? Wikipedia mentions that Schönhage–Strassen is advisable for numbers beyond 2^2^15 to 2^2^17. What to do when one number is ridiculously huge (e.g. 10,000 to 40,000 decimal digits), but second consists of just couple of digits?

Does all those 4 algorithms parallelizes easily?

like image 458
oski86 Avatar asked Jul 18 '15 11:07

oski86


People also ask

What is the type of algorithmic strategy used for large integer multiplication?

There are two ways to perform large integer multiplication using divide and conquer. The first method – we call dumb method – does not improve the running time. Second method – we call clever approach – performs better then the traditional approach for integer multiplication.

What is the fastest algorithm for multiplication of two n digit numbers?

The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm. The Toom–Cook algorithm (1963) is a faster generalization of Karatsuba's method, and the Schönhage–Strassen algorithm (1971) is even faster, for sufficiently large n.

What is the fastest integer multiplication algorithm?

For more than 35 years, the fastest known method for integer multiplication has been the Schönhage-Strassen algorithm running in time O(nlog nlog log n). Under certain restrictive conditions there is a corresponding Ω(nlog n) lower bound.

What is the name of an algorithm which is used to solve the largest number multiplication using divide and conquer?

Karatsuba algorithm for fast multiplication using Divide and Conquer algorithm. Given two binary strings that represent value of two integers, find the product of two strings.


1 Answers

You can browse the GNU Multiple Precision Arithmetic Library's source and see their thresholds for switching between algorithms.

More pragmatically, you should just profile your implementation of the algorithms. GMP puts a lot of effort into optimizing, so their algorithms will have different constant factors than yours. The difference could easily move the thresholds around by an order of magnitude. Find out where the times cross as input size increases for your code, and set the thresholds correspondingly.

I think all of the algorithms are amenable to parallelization, since they're mostly made up up of divide and conquer passes. But keep in mind that parallelizing is another thing that will move the thresholds around quite a lot.

like image 121
Craig Gidney Avatar answered Nov 01 '22 01:11

Craig Gidney