Karatsuba Algorithm | Brilliant Math & Science Wiki (2024)

Sign up with Facebook or Sign up manually

Already have an account? Log in here.

Karleigh Moore, Pranjal Jain, Dolly Ye, and

  • Andrew Dickson
  • Alex Chumbley
  • Alexander Crustev
  • Jimin Khim

contributed

The Karatsuba algorithm is a fast multiplication algorithm that uses a divide and conquer approach to multiply two numbers. The naive algorithm for multiplying two numbers has a running time of \(\Theta\big(n^2\big)\) while this algorithm has a running time of \(\Theta\big(n^{\log_2 3}\big)\approx \Theta\big(n^{1.585}\big)\). Being able to multiply numbers quickly is very important. Computer scientists often consider multiplication to be a constant time \(O(1)\) operation, and this is a reasonable simplification for smaller numbers; but for larger numbers, the actual running times need to be factored in, which is \(O\big(n^2\big)\). The point of the Karatsuba algorithm is to break large numbers down into smaller numbers so that any multiplications that occur happen on smaller numbers. Karatsuba can be used to multiply numbers in all base systems (base-10, base-2, etc.).

Contents

  • Naive Multiplication Algorithm
  • Karatsuba Algorithm
  • Implementation of the Karatsuba Algorithm
  • Complexity of Karatsuba
  • See Also
  • References

Naive Multiplication Algorithm

The naive way to multiple numbers is commonly taught in elementary school.

Karatsuba Algorithm | Brilliant Math & Science Wiki (1) Grade school multiplcation takes four multiplication steps

Here’s the naive multiplication algorithm to multiply two \(n\)-bit numbers, \(x\) and \(y\) that are in base \(b\).

Divide each number into two halves, the high bits \(H\) and the low bits \(L:\)

\[x = x_Hb^{\frac{n}{2}} + X_L, \quad y = y_Hb^{\frac{n}{2}} + Y_L.\]

Multiply the two numbers together:

\[\begin{align} xy &= \big(x_Hb^{\frac{n}{2}} + X_L\big) \times \big(y_Hb^{\frac{n}{2}} + Y_L\big)\\ &= x_Hy_Hb^n + (x_Hy_L + x_Ly_H)b^{\frac{n}{2}} + x_Ly_L. \end{align}\]

These formulas describe what is going on in the image above--the familiar grade-school way of multiplying numbers. This has four multiplications: \(x_Hy_Hb^n\), \((x_Hy_L)b^{\frac{n}{2}}\), \((x_Ly_H)b^{\frac{n}{2}}\), and \(x_Ly_L,\) which has a running time of \(O(n^2)\).

Let's use this method to multiply the base-10 numbers \(1234\) and \(8765:\)

\[\begin{align}x &= x_Hb^{\frac{n}{2}} + X_L \\&= 12 \times 10^2 + 34\\\\y &= y_Hb^{\frac{n}{2}} + Y_L\\&= 87 \times 10^2 + 65\\\\xy &= \big(x_Hb^{\frac{n}{2}} + X_L\big) \times \big(y_Hb^{\frac{n}{2}} + Y_L\big)\\&= (12 \times 10^2 + 34) \times (87 \times 10^2 +65)\\&= x_Hy_Hb^n + (x_Hy_L + x_Ly_H)b^{\frac{n}{2}} + x_Ly_L\\&= 12 \times 87 \times 10^4 + (12 \times 65 + 34 \times 87)\times 10^2 + (65 \times 34)\\& = 10,816,010.\end{align}\]

Karatsuba Algorithm

The Karatsuba algorithm decreases the number of subproblems to three and ends up calculating the product of two \(n\)-bit numbers in \(\Theta\big(n^{\log_2 3}\big)\) time--a vast improvement over the naive algorithm.

To multiply two \(n\)-bit numbers, \(x\) and \(y\), the Karatsuba algorithm performs three multiplications and a few additions, and shifts on smaller numbers that are roughly half the size of the original \(x\) and \(y\).

Here’s how the Karatsuba method works to multiply two \(n\)-bit numbers \(x\) and \(y\) which are in base \(b\).

Create the following three subproblems where \(H\) represents the high bits of the number and \(L\) represents the lower bits:[1]

See Also
Primes

  • \(a = x_Hy_H\)
  • \(d = x_Ly_L\)
  • \(e = (x_H + x_L)(y_H + y_L) - a -d \)
  • \(xy = ab^n + eb^{\frac{n}{2}} + d.\)

This only requires three multiplications and has the recurrence \[3T\left(\frac{n}{2}\right) + O(n) = O\left(n^{\log 3}\right).\] Karatsuba can be applied recursively to a number until the numbers being multiplied are only a single-digit long (the base case).

Divide and conquer techniques come in when Karatsuba uses recursion to solve subproblems--for example, if multiplication is needed to solve for \(a\), \(d\), or \(e\) before those variables can be used to solve the overall \(x \times y\) multiplication.

Perform the following multiplication using the Karatsuba method: \(1234 \times 4321\).[1]

First, determine the \(a\) value for step 1, \(a_1\)--this will contain the high bits of \(x\) and \(y\) since \(x\) and \(y\) have four bits, and the left-most two are the high bits.

\(a_1 = 12 \times 43\). Note, we will have to call the Karatsuba algorithm on \(a_1\) since a multiplication is necessary to obtain the value (this time, a two-bit multiplication). Before we recurse, though, let’s find \(d_1\) and \(e_1\).

\(d\) contains the lower bits of each number since \(x\) and \(y\) have four bits, and the lower bits in this problem are the two right-most bits.

\(d_1 = 34 \times 21\). Note, we will also have to recurse on \(d_1\) to obtain the value.

Recall that \(e= (x_H + x_L)(y_H + y_L) - a -d \).

\(e_1 = (12 + 34) \times (43+21) - a_1 - d_1\). Now we are stuck and can’t simplify \(e_1\) further until we have the values of \(a_1\) and \(d_1\), so it is time to recurse.

Solving for \(a_1:\)

We have\[\begin{align}a_1 &= 12 \times 43\\a_2 &= 1 \times 4 = 4\\d_2 &= 2 \times 3 = 6\\e_2 &= (1+2)(4+3) - a_2 - d_2\\ &= (1+2)(4+3) - 4 - 6\\ &= 11.\end{align}\]Recall that \(xy = ar^n + er^{\frac{n}{2}} + d.\) Therefore, \(a_1 = 12 \times 43 = 4 \times 10^2 + 11 \times 10 + 6 = 516\).

Solving for \(d_1:\)

We have\[\begin{align}d_1 &= 34 \times 21\\a_2 &= 3 \times 2 = 6\\d_2 &= 4 \times 1 = 4\\e_2 &= (3 + 4)(2+1) - a_2 - d_2 \\&= 11.\end{align}\]Since \(xy = ar^n + er^{\frac{n}{2}} + d,\) \(d_1 = 34 \times 21 = 6 \times 10^2 + 11 \times 10 + 4 = 714\).

Solving for \(e_1:\)

We have\[\begin{align}e_1 &= (46 \times 64) - a_1 - d_1\\a_2 &= 4 \times 6 = 24\\d_2 &= 6 \times 4 = 24\\e_2 &= (4+6)(6+4) - a_2 - d_2 \\&= 52.\end{align}\]Since \(xy = ar^n + er^{\frac{n}{2}} + d,\) \(e_1 = (46 \times 64) - a_1 - d_1 = 24 \times 10^2 + 52 \times 10 + 24 - 714 - 516= 1714\).

Now we can get the answer to the original problem as follows:

We have\[a_1 = 516,\quad d_1 = 714,\quad e_1 = 1714.\]Plugging into \(xy = ar^n + er^{\frac{n}{2}} + d\), we get\[xy = (516)10^4 + (1714)10^{2} + 714 = 5,332,114.\ _\square\]

Implementation of the Karatsuba Algorithm

Here is a Python implementation of the Karatsuba algorithm for base-10 numbers.

 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930
from math import ceil, floor#math.ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x.#math.floor(x) Return the floor of x as a float, the largest integer value less than or equal to x.def karatsuba(x,y): #base case if x < 10 and y < 10: # in other words, if x and y are single digits return x*y n = max(len(str(x)), len(str(y))) m = ceil(n/2) #Cast n into a float because n might lie outside the representable range of integers. x_H = floor(x / 10**m) x_L = x % (10**m) y_H = floor(y / 10**m) y_L = y % (10**m) #recursive steps a = karatsuba(x_H,y_H) d = karatsuba(x_L,y_L) e = karatsuba(x_H + x_L, y_H + y_L) - a - d return int(a*(10**(m*2)) + e*(10**m) + d)#print(karatsuba(12,12))#print(karatsuba(0,0))#print(karatsuba(1234,4321))#print(karatsuba(3141592653589793238462643383279502884197169399375105820974944592,# 2718281828459045235360287471352662497757247093699959574966967627))

Complexity of Karatsuba

To analyze the complexity of the Karatsuba algorithm, consider the number of multiplications the algorithm performs as a function of \(n\), \(M(n)\). Recall that the algorithm multiplies together two \(n\)-bit numbers. If \(n=2^k\) for some \(k\), then the algorithm recurses three times on \(\frac{n}{2}\)-bit number. The recurrence for this is

\[M(n) = 3M\left(\frac{n}{2}\right).\]

This takes care of the multiplications required for Karatsuba--now to consider the additions and subtractions. There are \(O(n)\) additions and subtractions required for the algorithm. Therefore, the overall recurrence for the Karatsuba algorithm is[2]

\[T(n) = 3T\left(\frac{n}{2}\right) + O(n).\]

Using the master theorem on the above recurrence yields that the running time of the Karatsuba algorithm is \(\Theta\big(n^{\log_2 3}\big)\approx \Theta\big(n^{1.585}\big).\)

The Schönhage–Strassen algorithm and the Toom–Cook algorithm are other popular integer multiplication algorithms. The Toom-Cook algorithm is faster, more generalized version of the Karatsuba algorithm that runs in \(\Theta\left(n^{\frac{\log 5}{\log 3}}\right) \approx \Theta\big(n^{1.465}\big)\).[3] The Schönhage–Strassen algorithm is faster than both Karatsuba and Toom-Cook for very large \(n\) \(\big(\)on the order of \(n > 2^{2^{15}}\big)\) and runs in \(O(n \log n \log \log n)\).[4]

See Also

  • Schönhage–Strassen Algorithm
  • Toom–Cook Algorithm

References

  1. Demaine, E., Indyk, P., & Kellis, M. Karatsuba’s Algorithm. Retrieved May 29, 2016, from http://courses.csail.mit.edu/6.006/spring11/exams/notes3-karatsuba
  2. Babai, L. Divide and Conquer: The Karatsuba–Ofman algorithm. Retrieved May 30, 2016, from http://people.cs.uchicago.edu/~laci/HANDOUTS/karatsuba.pdf
  3. , . Toom–Cook multiplication. Retrieved May 30, 2016, from https://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
  4. , . Schönhage–Strassen algorithm. Retrieved May 30, 2016, from https://en.wikipedia.org/wiki/Sch%C3%B6nhage%E2%80%93Strassen_algorithm

Cite as: Karatsuba Algorithm. Brilliant.org. Retrieved from https://brilliant.org/wiki/karatsuba-algorithm/

Karatsuba Algorithm | Brilliant Math & Science Wiki (2024)

FAQs

How does the Karatsuba algorithm work? ›

To multiply two n n n-bit numbers, x x x and y y y, the Karatsuba algorithm performs three multiplications and a few additions, and shifts on smaller numbers that are roughly half the size of the original x x x and y y y.

What is the complexity of the caratsuba algorithm? ›

In general, the Karatsuba Multiplication is said to have a time complexity of O(n^1.5...) . The algorithm assumes that the addition and subtraction take about O(1) each.

What is the history of the Karatsuba algorithm? ›

The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm. The Toom–Cook algorithm (1963) is a faster generalization of Karatsuba's method, and the Schönhage–Strassen algorithm (1971) is even faster, for sufficiently large n.

Which multiplication algorithm is best? ›

Karatsuba's algorithm was the first known algorithm for multiplication that is asymptotically faster than long multiplication, and can thus be viewed as the starting point for the theory of fast multiplications.

What is the fastest algorithm for matrix multiplication? ›

In linear algebra, the Strassen algorithm, named after Volker Strassen, is an algorithm for matrix multiplication. It is faster than the standard matrix multiplication algorithm for large matrices, with a better asymptotic complexity, although the naive algorithm is often better for smaller matrices.

What is the fastest algorithm for integer multiplication? ›

For more than 35 years, the fastest known method for integer multiplication has been the Schönhage-Strassen algorithm running in time O(nlog nlog log n).

What is the recurrence for Karatsuba's algorithm? ›

T(n) = 3i T(n/2i) is the general form of the recurrence relation of Karatsuba algorithm. T(n) = aT(n/b) + f(n), where, a = 3, b = 2 and f(n) = 0 which leads to k = 0.

Is Karatsuba recursive? ›

This is a recursive algorithm: during execution, it calls smaller instances of itself. This equation is a simple recurrence which we may solve directly as follows. Applying equation (1) to M(n/2) we obtain M(n/2) = 3M(n/4); therefore M(n) = 9M(n/4).

What is the most efficient algorithm complexity? ›

O(1): Constant time complexity — Most efficient time complexity because input size does not affect the algorithm's performance. O(log n): Logarithmic complexity. O(n): Linear complexity — Most common because input size is directly proportional to the algorithm's performance.

What is the fastest multiplication method? ›

Multiply from left to right. One digit at a time. Left to right multiplication is faster because you have to remember fewer numbers to recall and use later. You will immediately start calling out the answer from the very first step of the calculation.

What is the best method of multiplication? ›

One of the best and easy multiplication tricks for large numbers is to find the tens of one of the numbers, and multiply with that quickly. Adding the remaining leftovers will be easier to calculate fully. E.g., 22 X 83 can be rewritten as (20 X 83) + (2 X 83) which gives us 1660 + 166 = 1826.

What is the oldest known algorithm? ›

The oldest recorded non-trivial algorithm is the Euclidean algorithm for the greatest common divisor of two positive integers.

What is the main idea of the caratsuba algorithm? ›

The Karatsuba algorithm is a fast divide-and-conquer algorithm that reduces the number of recursive multiplications needed to multiply larger integers, improving the overall efficiency. Let's say we want to multiply two n-digit integers x and y, where n is a power of 2.

How to multiply billions? ›

To multiply two numbers with 1 billion digits requires 1 billion squared, or 1018, multiplications, which would take a modern computer roughly 30 years.

What is the space complexity of the caratsuba algorithm? ›

Karatsuba's algorithm only requires O(n) space. Here is a rough inductive argument. Inductively, suppose multiplying n-digit numbers using Karatsuba's algorithm takes only cn space.

How does Kociemba algorithm work? ›

Kociemba's algorithm, then, is to take the original position, call it p, compute r(p), the relabeling; solve the relabeled puzzle with some sequence a ∈ S∗, ap- ply those moves to an original cube yielding pa which lies in H, and then finish the solution with another sequence b ∈ A∗ such that pab is the solved cube.

How does the Babylonian algorithm work? ›

method itself is very simple: if you want to calculate √p, choose any initial value as your first guess, call it x, and then iterate by repeatedly finding a new value for x according to the following formula. Basically, the idea is that if x is greater (smaller) than √p, then p/x will be smaller (greater) than √p.

How does this algorithm work? ›

How do algorithms work? Algorithms use a set of initial data or input, process it through a series of logical steps or rules, and produce the output (i.e., the outcome, decision, or result).

How does the multiplication algorithm work? ›

Standard algorithm means the process that is generally accepted and used by most people. The standard algorithm for multiplication involves multiplying each place value by each other and then adding the answers.

Top Articles
Cape Air Partner United Airlines
Numeracy, Maths and Statistics - Academic Skills Kit
Teleport Pads Disabled In Garden
Beekman Hsn Schedule
Bofa Financial Center Near Me
Costco Gas Prices Lansing
M Life Insider
Studentvue Ccboe Login
[H] Wasteland 3, Cuphead, Resident Evil 7, Cloudpunk, Firewatch, Heavy Rain [W] Offers, Skins
San Diego Terminal 2 Parking Promo Code
How To Play BucketBall
Waitlistcheck Sign Up
Acbl Homeport
Espn Auction Values
Tw's Bait And Tackle Fishing Report
Panama City News Herald Obituary
Culver's Flavor Of The Day Ann Arbor
Lesson 12 Homework 4.5 Answer Key
Manhungay
Curaleaf Bell Leafly
Pge Outage Map Beaverton
Walgreens Colesville
Oodweynenews
Classy Spa Fort Walton Beach
Gobluecc Sports
Dtm Urban Dictionary
Citibank Branch Locations In Orlando Florida
2005 Chevrolet Silverado Radio Wiring Diagram
Papa Johns Mear Me
Pewdiepieisprettydarncool
Randash Belgrade
Wausau Craigslist Marketplace
Google Sites Among Us
Unitedhealthcare Hwp
Genesis Fs Card Services Kay
Kaiser Hesperia Laboratory Hours
Palm Beach Tan Nashville
32,000+ Las Vegas jobs in United States
Kallmekris Rape
Please Help Me: What to Do When You Need Help
Wall Street Institute
A Compressed Work Week Provides All Of The Following Except
Honeybee: Classification, Morphology, Types, and Lifecycle
The Weather Channel - Radar
Vlad The Impaler Dick Size
Webkinz® - Top Issues
Foxes Are Amazing 99.Github Io
Egusd Lunch Menu
Opscans 1073
Td Bank Hours Weekend
با دیدنی های نورنبرگ آلمان بیشتر آشنا شویم - سفری دیگر
Worldfree4U Movies In
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 6314

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.