Implementing Karatsuba Algorithm in Python
Primary Objectives
- Implement the Karatsuba Method
- Do not use any
*operator (like -- not at all!)
First, let's import the math library.
Let's add some util functions for adding zeros. The following operation is super-expensive, and I did this for the sake of removing *s.
If you do not care about not using *s, you can go with:
Let's say the standard input provides the value in string, with , in between the two numbers. I wrote a wrapper class that parses the standard input and feeds the value into the core method.
Then we need to finish the actual calculation. For the base calculation (the line after if min(v1, v2) <= 100:) you could go with v1 * v2 if you don't need to remove *s.
It is always a good idea to have some validation. Unfortunately, I did not use any testing library; this short script will suffice the purpose of validating the answer.
If you run this, you will get: