问题
multiplication of two numbers
x*y ----> x =(x0*10^(n/3)+x1*10^(n/3)+x2) and y=(y0*10^(n/3)+y1*10^(n/3)+y2)
It is 9 multiplication of 10^n/3 numbers so 9T(n/3) but it can be reduced to 5 by the following method.
x*y= x0*y0+x1*y1+x2*y2+x0*y1+x0*y2+x1*y0+x1*y3+x2*y0+x2*y1
I am able to reduce the multiplication of two numbers to 5T(n/3) by similar trick like Karatsuba's algorithm
(x0+x1+x2)(y0+y1+y2)-x0*y0-x1*y1-x2*y2= x0*y1+x0*y2+x1*y0+x1*y3+x2*y0+x2*y1
All and all 5 multiplication of n/3 bits number so 5T(n/3)+O(n) but how can I do in 6 multiplication like 6T(n/3)+O(n)
Q1 Can it be reduced to 6 instead of 5 ?
[edit1] correction copied from the reasked question by Spektre
x and y has n bits
x=x0*(10^2n/3)+x1*10^n/3+x2
y=y0*(10^2n/3)+y1*10^n/3+y2
x*y=x2y2+(x2y1+x1y2)10^n/3+(x2y0+x1y1+x0y2)10^2n/3+(x1y0+x0y1)10^n+x0y0*10^4n/3
- now 9 multiplication of n/3 bit numbers running time 9T(n/3) which is O(n^2)
with little trick like Karatsuba's multiplication:
- first calculate x0y0, x1y1 and x2y2 this is 3 multiplication of n/3 bit numbers
- then use x0y0, x1y1 and x2y2 to calculate the others follow:
x2y1+x1y2=(x1+x2)(y1+y2)-x1y1-x2y2->> 1 multiplication of n/3 bit numberx2y1+x1y1+x0y2=(x0+x2)(y0+y2)-x0y0-x2y2+x1y1->> 1 multiplication of n/3 bit numberx1y0+x0y1=(x0+x1)(y0+y1)-x0y0-x1y1->> 1 multiplication of n/3 bit number
Recursively solve the 6 subproblems
- and combine them with 7 additions on O(n)-digit numbers.
- total it takes now 6 multiplication of n/3 bit numbers running time 6T(n/3)
Q2 how can I reduce this to 5 multiplication instead of 6 ?
- Q1 is now obsolete due to mistake in OP
回答1:
Not an answer but this would be unreadable as comment
if you divide digits to 3 groups then greybeard is right
a0=10^(2n/3)
a1=10^(1n/3)
a2=10^(0n/3)
x=x0*a0+x1*a1+x2*a2
y=y0*a0+y1*a1+y2*a2
so multiplication looks like this instead:
x*y=a0(x2*y2+x1*y2+x0*y2)
+a1(x2*y1+x1*y1+x0*y1)
+a2(x2*y0+x1*y0+x0*y0)
rewrite to digits:
b0=(x2*y2+x1*y2+x0*y2)
b1=(x2*y1+x1*y1+x0*y1)
b2=(x2*y0+x1*y0+x0*y0)
x*y=a0*b0+a1*b1+a2*b2
and now simplify b0,b1,b2
[hints]
- what trick did you exploit?
- why is in your result no digit weight a0,a1,a2?
[edit1] if you want to solve this then use standard notation instead of yours
lower significant digits are lower index so:
a0=10^(0n/3)
a1=10^(1n/3)
a2=10^(2n/3) // this is max wieght for operand
a3=10^(3n/3)
a4=10^(4n/3)
a5=10^(5n/3) // this is max weight for multiplication result
x=x0*a0+x1*a1+x2*a2
y=y0*a0+y1*a1+y2*a2
so multiplication looks like this instead:
x*y=a0(x0*y0+x1*y0+x2*y0)
+a1(x0*y1+x1*y1+x2*y1)
+a2(x0*y2+x1*y2+x2*y2)
rewrite to digits:
b0=(x0*y0+x1*y0+x2*y0)
b1=(x0*y1+x1*y1+x2*y1)
b2=(x0*y2+x1*y2+x2*y2)
x*y=a0*b0+a1*b1+a2*b2
of coarse b0,b1,b2 are not in range of single digit - instead they are all double digit due to multiplication
so you have to split them into singular digits:
x*y=a0*B0+a1*B1+a2*B2+a3*B3+a4*B4+a5*B5
where:
B0=(b0/a0)%a1
B1=(b0/a1+b1/a0)%a1
B2=(b0/a2+b1/a1+b2)%a1
B3=(b0/a3+b1/a2+b2/a1)%a1
B4=(b0/a4+b1/a3+b2/a2)%a1
B5=(b0/a5+b1/a4+b2/a3)%a1
- and also do not forget to handle overflows...
来源:https://stackoverflow.com/questions/26209076/multiplication-of-two-numbers-in-6-of-n-3-bits-6tn-3-karatsuba