ArticleslgStudy

computer science

Karatsuba algorithm

Karatsuba algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Karatsuba algorithm rather than just read about it. In short: The Karatsuba algorithm is a fast multiplication algorithm for integers. It was discovered by Anatoly Karatsuba in 1960 and published in 1962.

Karatsuba algorithm — main illustration
Karatsuba algorithm — illustration

Key takeaways

  • Karatsuba algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Karatsuba algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Karatsuba algorithm from memory before moving on to harder problems.

Reference excerpt

The Karatsuba algorithm is a fast multiplication algorithm for integers. It was discovered by Anatoly Karatsuba in 1960 and published in 1962. It is a divide-and-conquer algorithm that reduces the multiplication of two n-digit numbers to three multiplications of n/2-digit numbers and, by repeating this reduction, to at most nlog23≈n1.58 single-digit multiplications. It is therefore asymptotically faster than the traditional algorithm, which performs n2 single-digit products. The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm.

History The standard procedure for multiplication of two n-digit numbers requires a number of elementary operations proportional to n2, or O(n2) in big-O notation. Andrey Kolmogorov conjectured that the traditional algorithm was asymptotically optimal, meaning that any algorithm for that task would require Ω(n2) elementary operations. In 1960, Kolmogorov organized a seminar on mathematical problems in cybernetics at the Moscow State University, where he stated the Ω(n2) conjecture and other problems in the complexity of computation. Within a week, Karatsuba, then a 23-year-old student, found an algorithm that multiplies two n-digit numbers in O(nlog23) elementary steps, thus disproving the conjecture. Kolmogorov was very excited about the discovery; he communicated it at the next meeting of the seminar, which was then terminated. Kolmogorov gave some lectures on the Karatsuba result at conferences all over the world (see, for example, "Proceedings of the International Congress of Mathematicians 1962", pp. 351–356, and also "6 Lectures delivered at the International Congress of Mathematicians in Stockholm, 1962") and published the method in 1962, in the Proceedings of the USSR Academy of Sciences. The article had been written by Kolmogorov and contained two results on multiplication, Karatsuba's algorithm and a separate result by Yuri Ofman; it listed "A. Karatsuba and Yu. Ofman" as the authors. Karatsuba only became aware of the paper when he received the reprints from the publisher.

Algorithm

Basic step The basic principle of Karatsuba's algorithm is divide-and-conquer, using a formula that allows one to compute the product of two large numbers x and y using three multiplications of smaller numbers, each with about half as many digits as x or y, plus some additions and digit shifts. This basic step is, in fact, a generalization of a similar complex multiplication algorithm, where the imaginary unit i is replaced by a power of the base. Let x and y be represented as n-digit strings in some base B. For any positive integer m less than n, one can write the two given numbers as

x=x1Bm+x0, y=y1Bm+y0, where x0 and y0 are less than Bm and x1 and y1 are less than Bn−m. The product is then xy=(x1Bm+x0)(y1Bm+y0)=x1y1B2m+(x1y0+x0y1)Bm+x0y0=z2B2m+z1Bm+z0, where

z2=x1y1, z1=x1y0+x0y1, z0=x0y0. These formulae require four multiplications and were known to Charles Babbage. Karatsuba observed that xy can be computed in only three multiplications, at the cost of a few extra additions. With z0 and z2 as before and z3=(x1+x0)(y1+y0), one observes that

z1=x1y0+x0y1=x1y0+x0y1+x1y1+x0y0−x1y1−x0y0=(x1+x0)(y1+y0)−x1y1−x0y0=z3−z2−z0. Thus only three multiplications are required for computing z0,z1 and z2, and thus xy. An alternative form uses z4=(x1−x0)(y1−y0) instead:

z1=x1y1+x0y0−(x1y1−x0y0+x1y0+x0y1=x1y1+x0y0−(x1y1−x0y0+x1y0+x0y1=x1y1+x0y0−(x1y1+x0y0−x1y0−x0y1)=x1y1+x0y0−(x1−x0)(y1−y0)=z2+z0−z4.

Example To compute the product of 12345 and 6789, where B = 10, choose m = 3. We use m right shifts for decomposing the input operands using the resulting base (Bm = 1000), as:

12345 = 12 · 1000 + 345 6789 = 6 · 1000 + 789 Only three multiplications, which operate on smaller integers, are used to compute three partial results:

z2 = 12 × 6 = 72 z0 = 345 × 789 = 272205 z1 = (12 + 345) × (6 + 789) − z2 − z0 = 357 × 795 − 72 − 272205 = 283815 − 72 − 272205 = 11538 We get the result by just adding these three partial results, shifted accordingly (and then taking carries into account by decomposing these three inputs in base 1000 as for the input operands):

result = z2 · (Bm)2 + z1 · (Bm)1 + z0 · (Bm)0, i.e. result = 72 · 10002 + 11538 · 1000 + 272205 = 83810205. Note that the intermediate third multiplication operates on an input domain which is less than two times larger than for the two first multiplications, its output domain is less than four times larger, and base-1000 carries computed from the first two multiplications must be taken into account when computing these two subtractions.

Recursive application If n is four or more, the three multiplications in Karatsuba's basic step involve operands with fewer than n digits. Therefore, those products can be computed by recursive calls of the Karatsuba algorithm. The recursion can be applied until the numbers are so small that they can (or must) be computed directly. In a computer with a full 32-bit by 32-bit multiplier, for example, one could choose B = 231 and store each digit as a separate 32-bit binary word. Then the sums x1 + x0 and y1 + y0 will not need an extra binary word for storing the carry-over digit (as in carry-save adder), and the Karatsuba recursion can be applied until the numbers to multiply are only one digit long.

Time complexity analysis

Karatsuba's basic step works for any base B and any m, but the recursive algorithm is most efficient when m is equal to n/2, rounded up. In particular, if n is 2k, for some integer k, and the recursion stops only when n is 1, then the number of single-digit multiplications is 3k, which is nc where c = log23. Since one can extend any inputs with zero digits until their length is a power of two, it follows that the number of elementary multiplications, for any n, is at most 3⌈log2n⌉≤3nlog23. Since the additions, subtractions, and digit shifts (multiplications by powers of B) in Karatsuba's basic step take time proportional to n, their cost becomes negligible as n increases. More precisely, if T(n) denotes the total number of elementary operations that the algorithm performs when multiplying two n-digit numbers, then

… excerpt ends here. Continue reading the full article.

Illustrations

Karatsuba algorithm: Karatsuba multiplication of az+b and cz+d (boxed), and 1234 and 567 with z=100. Magenta arrows denote multiplication, amber denotes addition, silver denotes subtraction and cyan denotes left shift. (A), (B) and (C) show recursion with z=10 to obtain intermediate values.
Karatsuba multiplication of az+b and cz+d (boxed), and 1234 and 567 with z=100. Magenta arrows denote multiplication, amber denotes addition, silver denotes subtraction and cyan denotes left shift. (A), (B) and (C) show recursion with z=10 to obtain intermediate values.

Worked examples

Example 1 — a first encounter with Karatsuba algorithm

Start with the simplest possible case. Write down what Karatsuba algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Karatsuba algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Karatsuba algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Karatsuba algorithm

In research
Karatsuba algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Karatsuba algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Karatsuba algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic algorithms, Divide-and-conquer algorithms, Multiplication, so understanding it makes those chapters shorter.
In everyday life
Look for Karatsuba algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Karatsuba algorithm” →

How to study Karatsuba algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Karatsuba algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Karatsuba algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Karatsuba algorithm in simple terms?

The Karatsuba algorithm is a fast multiplication algorithm for integers. It was discovered by Anatoly Karatsuba in 1960 and published in 1962.

Why does Karatsuba algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Karatsuba algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Karatsuba algorithm.

Tags

  • Computer arithmetic algorithms
  • Divide-and-conquer algorithms
  • Multiplication

Keep exploring