1

I'm having trouble to implement the karatsuba algorithm in python. I'm working with lists in base 2 (the MSB is at the end in the list). The implementation given to me is this:

Input: 2 bit-numbers of bit length n
Output: their product a*b

function Karatsuba(a, b) 
     if(n == 1) return a*b
     else 
          a1, a0, leftmost(n/2)(rounded up), rightmost(n/2)(rounded down) bits of a
          b1, b0, leftmost(n/2)(rounded up), rightmost(n/2)(rounded down) bits of b

     s1 = Karatsuba(a1, b1)
     s2 = Karatsuba(a0, b0)
     s3 = Karatsuba(a1 + a0, b1 + b0)

     return s1 * 2^n + (s3 - s1 - s2) * 2^(n/2) + s2

and this my python implementation:

def karatsuba(A, B):
    if(len(A) == 1 or len(B) == 1):
        return Multiply(A, B)

    n = max(len(A), len(B))
    m = n / 2

    print "Karatsuba call"

    print "A", A, "\n"
    print "B", B, "\n"

    lowA = A[:m]
    highA = A[m:]

    lowB = B[:m]
    highB = B[m:]

    print "highA", highA, "\n"
    print "lowA", lowA, "\n"

    print "highB", highB, "\n"
    print "lowB", lowB, "\n"

    s1 = karatsuba(highA, highB)
    s2 = karatsuba(lowA, lowB)
    s3 = karatsuba(Add(highA, lowA), Add(highB, lowB))

    f1 = Multiply(s1, pow2(n))
    f2 = Multiply(Sub(Sub(s3, s1), s2), pow2(m))
    return Add(f1, Add(f2, s2))

However running with the input (remember the MSB is the rightmost bit):

A [0, 1, 1] 
B [0, 1, 1] 

I get Product Karatsuba [0, 0, 0, 1, 0, 0, 1, 0] 72 but it should output [0, 0, 1, 0, 0, 1] 36 . The functions Add, Substract, pow2 and Multiply are working, I have tested them separately. If it helps here's the full output with the print statements:

Karatsuba call
A [0, 1, 1] 

B [0, 1, 1] 

highA [1, 1] 

lowA [0] 

highB [1, 1] 

lowB [0] 

Karatsuba call
A [1, 1] 

B [1, 1] 

highA [1] 

lowA [1] 

highB [1] 

lowB [1] 

Karatsuba call
A [0, 1] 

B [0, 1] 

highA [1] 

lowA [0] 

highB [1] 

lowB [0] 

Karatsuba call
A [1, 1] 

B [1, 1] 

highA [1] 

lowA [1] 

highB [1] 

lowB [1] 

Karatsuba call
A [0, 1] 

B [0, 1] 

highA [1] 

lowA [0] 

highB [1] 

lowB [0] 

I'm searching for hours, and I have no more idea where my error is. Can somebody help me? Thanks

user2336315
  • 15,697
  • 10
  • 46
  • 64
  • @greybeard Sorry I'm not sure I get what you mean? – user2336315 Feb 25 '15 at 08:49
  • 1
    Are you sure that your arithmetic functions are giving correct results? If so, then how come there is a zero in the MSB of your final result? shouldn't it always be a 1 (except if the value is zero)? – samgak Feb 25 '15 at 09:32
  • @samgak yes they were given to me; I just add to implement pow2n which is correct (I can show it if you want). I don't know why there is this extra 0. – user2336315 Feb 25 '15 at 13:24
  • 1
    yes please post it. Your print statement output looks correct to me, so I suspect it's something other than the code you have posted. Also what logs do you get if you assign Add(f1, Add(f2, s2)) to a variable and print it out before returning from the function? – samgak Feb 25 '15 at 13:46
  • @samgak I tried to compute this with pen and pencil, with the algorithm description. I get as final result 9*2^3 which is indeed 72. So either I misunderstand the algorithm, or it's an error in the description I gave (which would be weird because it's my lesson slide). Could it be possible? – user2336315 Feb 25 '15 at 14:54

1 Answers1

2

The error is this one:

f1 = Multiply(s1, pow2(n))

It should be:

f1 = Multiply(s1, pow2(2*m))

Indeed, (a1*2^m+a0)*(b1*2^m+b0)=(a1*b1)*2^(2m) + (a0*b1+a1*b0)*2^m + (a0*b0)

If n > (2*m), that is for an odd n, then you are doing things incorrectly...

aka.nice
  • 9,100
  • 1
  • 28
  • 40
  • Oh! Many thanks! I guess I did the same error when performing the algo on my paper! Now it works :) – user2336315 Feb 25 '15 at 18:13
  • So to clarify; the pseudo code is a bit misleading right? It says it should be s1 * 2^n but in reality it's s1 ^ 2*(n/2) and due to integer division it does not produce the same result right? I though I could make the same assumption but apparently I was wrong.... – user2336315 Feb 25 '15 at 18:15
  • Afterthoughts, this sounds like similar to http://stackoverflow.com/questions/17531042/karatsuba-algorithm-without-biginteger-usage/22182739#22182739 – aka.nice Feb 25 '15 at 18:47