Karatsuba Multiplication.

The Karatsuba algorithm works by reducing a very large multiplication into several multiplications and additions of numbers much smaller than the originals. This significantly speeds up the processing when multiplying very large numbers.

Consider integers x and y where both are n digits in size (if they are not, then add zeros to the front of the smaller number to make it so). Let’s also choose a number, m which is less than n. Then:

x = x1 x 10m + x0      and x0 < 10m

y = y1 x 10m + y0      and y0 < 10m

So

x y = (x1 x 10m + x0) (y1 x 10m + y0)

      = x1y1 x 102m + x1y0 x 10m + x0y1 x 10m + x0y0

      = x1y1 x 102m + (x1y0 + x0y1) x 10m + x0y0

This requires four multiplications where each multiplication is of numbers having fewer digits than the original n.

Now

x1y0 + x0y1 = (x1 + x0)(y0 + y1) - x1y1 - x0y0

which turns these two multiplications into three but two of them are the same as calculated above; namely x1y1 and x0y0.

Hence

xy = x1y1 x 102m + [(x1 + x0)(y0 + y1) - x1y1 - x0y1] x 10m + x0y0

Hence, we end up calculating the original x times y with three other multiplications plus a few additions. But note that each of these multiplications takes place between numbers having fewer digits than the original. If m is chosen to be half of n then the multiplication takes place with numbers half the size of the original.

An example may help solidify this in your mind.

x = 345,678

y = 3,875

here n is 6, the number of digits in the larger number. Let's pick m = 3, about one half of n. Then

x = 345 x 1000 + 678      where x1 = 345 and x0 = 678

y = 3 x 1000 + 875          where y1 = 3 and y0 = 875

So

x1y1 = 1,035                x1y0 = 301,875

x0y0 = 593,250            x0y1 = 2,034

and

x1y0 + x0y1 = (x1 + x0) (y1 + y0) - x1y1 - x0y0

                   = (345 + 678) x (3 + 875) - 1,035 - 593,250 = 898,194 - 1,035 - 593,250 = 303,909

so xy = 1,035 x 106 + 303,900 x 103 + 593,250

         = 1,035 x 1,000,000 + 303,909 x 1000 + 593,250 = 1,339,502,250

Using a power of 10 means that instead of multiplying, we are simply shifting left and inserting zeros.

Now all this extra shifting and adding causes a bit of overhead for the computer so Karastuba multiplication isn't done for small numbers. In its implementation on this site, Karatsuba isn't invoked unless both numbers consist of at least 900 digits each.