Content deleted Content added
→Implementation: Changed the calculation of the size of numbers in pseudocode. We need the max lenght of num1 or num2, not the minimum. |
→Implementation: format code |
||
Line 103:
The second argument of the split_at function specifies the number of digits to extract from the ''right'': for example, split_at("12345", 3) will extract the 3 final digits, giving: high="12", low="345".
<syntaxhighlight lang="
function karatsuba
if (num1 < 10
return num1 × num2 /* fall back to traditional multiplication */
/* Calculates the size of the numbers. */
m = max
m2 = floor
/* m2 = ceil (m / 2) will also work */
/* Split the digit sequences in the middle. */
high1, low1 = split_at
high2, low2 = split_at
/* 3 recursive calls made to numbers approximately half the size. */
z0 = karatsuba
z1 = karatsuba
z2 = karatsuba
return (z2 × 10 ^ (m2 × 2)) + ((z1 - z2 - z0) × 10 ^ m2) + z0
|