-1

I am trying to implement the Karatsuba algorithm in C. I work with char strings (which are digits in a certain base), and although I think I have understood most of the Karatsuba algorithm, I do not get where I should split the strings to multiply.

For example, where should I cut 123 * 123, and where should I cut 123 * 12?
I can't get to a solution that works with both these calculations.

I tried to cut it in half and flooring the result when the number if odd, but it did not work, and ceiling does not work too.

Any clue?

Let a, b, c, and d be the parts of the strings.

  • Let's try with 123 * 12

    First try (a = 1, b = 23, c = 1, d = 2) (fail)

    z0 = a * c = 1
    z1 = b * d = 46
    z2 = (a + b) * (c + d) - z0 - z1 = 24 * 3 - 1 - 46 = 72 - 1 - 46 = 25
    
    z0_padded = 100
    z2_padded = 250
    
    z0_padded + z1 + z2_padded = 100 + 46 + 250 = 396 != 123 * 12
    

    Second try (a = 12, b = 3, c = 12, d = 0) (fail)

    z0 = 144
    z1 = 0
    z2 = 15 * 12 - z1 - z0 = 180 - 144 = 36
    
    z0_padded = 14400
    z2_padded = 360
    
    z0_padded + z1 + z2_padded = 14760 != 1476
    

    Third try (a = 12, b = 3, c = 0, d = 12) (success)

    z0 = 0
    z1 = 36
    z2 = 15 * 12 - z0 - z1 = 144
    
    z0_padded = 0
    z2_padded = 1440
    
    z0_padded + z1 + z2_padded = 1476 == 1476
    
  • Let's try with 123 * 123

    First try (a = 1, b = 23, c = 1, d = 23) (fail)

    z0 = 1
    z1 = 23 * 23 = 529
    z2 = 24 * 24 - z0 - z1 = 46
    
    z0_padded = 100
    z2_padded = 460
    
    z0_padded + z1 + z2_padded = 561 != 15129
    

    Second try (a = 12, b = 3, c = 12, d = 3) (success)

    z0 = 12 * 12 = 144
    z1 = 3 * 3 = 9
    z2 = 15 * 15 - z0 - z1 = 72
    
    z0_padded = 14400
    z2_padded = 720
    
    z0_padded + z1 + z2_padded = 15129 == 15129
    

    Third try (a = 12, b = 3, c = 1, d = 23) (fail)

    z0 = 12
    z1 = 3 * 23 = 69
    z2 = 15 * 24 - z0 - z1 = 279
    
    z0_padded = 1200
    z2_padded = 2799
    
    z0_padded + z1 = z2_padded = 4068 != 15129
    

Here, I do not get where I messed this up. Note that my padding method adds n zeroes at the end of a number where n = m * 2 and m equals the size of the longest string divided by two.

EDIT

Now that I have understood that b and d must be of the same length, it works almost everytime, but there are still exceptions: for example 1234*12

a = 123
b = 4
c = 1
d = 2

z0 = 123
z1 = 8
z2 = 127 * 3 - 123 - 8 = 250

z0_padded = 1230000
z2_padded = 25000

z0_padded + z1 + z2_padded = 1255008 != 14808

Here, assuming I split the strings correctly, the problem is the padding, but I do not get how I should pad. I read on Wikipedia that I should pad depending on the size of the biggest string (see a few lines up), there should be another solution.

greybeard
  • 2,249
  • 8
  • 30
  • 66
  • 1
    "Does not work" is not a very helpful description. It would be better if you described clearly what you're doing, what you expect to happen, and what actually happens instead. See the [help center](http://stackoverflow.com/help/how-to-ask) for more tips on asking good questions. – Ilmari Karonen Nov 07 '14 at 19:31
  • `First try (a = 1, b = 23, c = 1, d = 23)`, `z0_padded = 100` is a mistake. `z0_padded` should be 10000. As long as `b` and `d` are equal lengths and you pad correctly, it should work. – JS1 Nov 07 '14 at 20:10
  • Please write down in which situation you are padding on the left (and by how much), and the situation(s) where you pad on the right - _why_ do you pad on the right, _what does it mean_? (Oh, and please don't make your readers (including your later self) guess what padding you are referring to.) – greybeard Nov 08 '14 at 17:42
  • This question has the names *z₀, z₁, and z₂* permuted with respect to the pseudocode on en.wikipedia. (The latter is consistent with the power of the base associated with each *zₓ* term.) – greybeard Mar 24 '19 at 19:24

1 Answers1

2

The Karatsuba algorithm is a nice way to perform multiplications.
If you want it to work, b and d must be of the same length.

Here are two possibilities to compute 123x12 :

a= 1;b=23;c=0;d=12;
a=12;b= 3;c=1;d= 2;

Let's explain how it works for the second case :

123=12×10+3
 12= 1×10+2
123×12=(12×10+3)×(1×10+2)
123×12=12×1×100+             (12×2+3×1)×10+3×2
123×12=12×1×100+((12+3)×(1+2)-12×1-3×2)×10+3×2

Let's explain how it works for the first case :

123=1×100+23
 12=0×100+12
123×12=(1×100+23)×(0×100+12)
123×12=1×0×10000+              (1×12+23×0)×100+23×12
123×12=1×0×10000+((1+23)×(0+12)-1×0-23×12)×100+23×12

It also works with 10^k, 2^k or n instead of 10 or 100.

greybeard
  • 2,249
  • 8
  • 30
  • 66
francis
  • 9,525
  • 2
  • 25
  • 41
  • I did not know that b and d must have the same length. Is works well now, but there's still a problem, let's say you want to compute "1234*12" (a = 123, b = 4, c = 1, d = 2), z0 = ac = 123, and when I pad it (with 4 zeros in base ten) i will get a result much more larger than the real result – user3574376 Nov 08 '14 at 12:07
  • (The fourth lines of both explanation blocks show the terms of "schoolbook multiplication" (count 4 `×`(+2 "scaling ×")), the fifth lines those of Karatsuba's algorithm - while there are 5 `×`(+2 "scaling ×"), note two pairs of identical products occurring in terms of different weight.) – greybeard Mar 24 '19 at 18:23