1

I tried two methods. The first was to pad X and Y with 0s so they both become of even and equal length, for example:

X = 123 , Y = 45678

becomes:

X = 000123 , Y = 045678

a = 0 , b = 123 , c = 45 , d = 678

but the problem with this implementation is when inputting:

X = 100 and Y = 14

becomes:

X = 0100 , Y = 0014

a = 1 , b = 0 , c = 0 , d = 14

ac = 0

bd = 0

ad + bc = 14

then: 10^4*(0) + 10^2*(14) + 0

notice how 10^2(14) is exactly the problem we started with, this will cause infinite recursion

the second solution I tried was to pad 0s to the right side instead, and then divide the final answer by 10^(# of 0s added) but that also causes infinite recursion in some scenarios.

What should I do?

  • 3
    10^2 * 14 is *not* the same problem you started with, because that 10^2 is *known* to be a power of 10, and you're going to do the multiply by just adding two zeros to 14 instead of calling Karatsuba multiplication recursively. – Matt Timmermans Aug 15 '21 at 22:35
  • (While *Karatsuba multiplication* can be viewed as an operation on polynomials that works using "any" "base", you purposefully choose one where "scaling" is cheap/trivial.) – greybeard Aug 16 '21 at 05:01

0 Answers0