0% found this document useful (0 votes)
4 views22 pages

Karatsuba Algorithm

The Karatsuba algorithm is a fast multiplication method that uses a divide and conquer approach to improve the efficiency of multiplying large integers, reducing the time complexity from O(n^2) to O(n^log2(3)). It operates by splitting the numbers into smaller parts and performing three recursive multiplications, which are then combined to produce the final result. This algorithm is particularly advantageous for large numbers due to its lower time complexity compared to traditional multiplication methods.

Uploaded by

samb.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Karatsuba Algorithm

The Karatsuba algorithm is a fast multiplication method that uses a divide and conquer approach to improve the efficiency of multiplying large integers, reducing the time complexity from O(n^2) to O(n^log2(3)). It operates by splitting the numbers into smaller parts and performing three recursive multiplications, which are then combined to produce the final result. This algorithm is particularly advantageous for large numbers due to its lower time complexity compared to traditional multiplication methods.

Uploaded by

samb.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

TEST TIME ON EUCLID’S ALGORITHM

URL:https://forms.gle/2Qag3HhHTrpx5fna8
QR CODE:
KARATSUBA
ALGORITHM
Karatsuba Algorithm

Introduction

The Karatsuba algorithm is a fast multiplication algorithm.


It was discovered by Anatoly Karatsuba in 1960 and published
in 1962.

A fast multiplication algorithm uses a divide and conquer


approach to multiply two numbers.

The naive algorithm for multiplying 2 numbers has a running


time of O(n2), where karatsuba algorithm has a runtime of
Karatsuba Algorithm

Introduction

Being able to multiply numbers quickly is very important.


Computer scientists often consider multiplication to be a
constant time ~ O(1) operation which is reasonable for small
numbers.

Whereas for larger numbers, the actual running times need to


be factored in, which is O(n2)
Karatsuba Algorithm

Naive
Method
The naive method is to follow the elementary school
multiplication method, i.e. to multiply each digit of
the second number with every digit of the first
number and then add all the multiplication results.
This algorithm takes O(n2) time.
Naive Method Karatsuba Algorithm
public static int multiplication(int X, int carry = num / 10;
Y) { }
// Convert numbers into string // else the digit is append to
String x = Integer.toString(X); the intermediate result
String y = Integer.toString(Y); // And assign carry as
int result = 0; zero
// Looping over y else {
for (int i = 0; i < y.length(); i++) { inter_res =
int carry = 0; // intermediate carry Integer.toString(num) +
String inter_res = ""; // intermediate inter_res;
result carry = 0;
// Looping over x. }
for (int j = x.length() - 1; j >= 0; }
j--) { // Adding the intermediate
// intermediate multiplication of results
each digit and addition of carry. result *= 10;
int num = result +=
Character.getNumericValue(y.charAt(i)) * Integer.parseInt(inter_res);
Character.getNumericValue(x.charAt(j)) + }
carry; System.out.print("result:
Karatsuba Algorithm

Karatsuba Algorithm
Let’s consider two 4-digit numbers x and y where
x=1234 and y=5678.
First of all, we should divide the n-digit numbers
into n/2-digit numbers as shown below.

a and c represent the first n/2 digits of x and


y. Similarly, b and d represent the last n/2 digits of x
and y
Karatsuba Algorithm

The Karatsuba algorithm involves 4 main steps

Step 1: Compute a.c = 12 x 56 = 672


Step 2: Compute b.d = 34 x 78 = 2652
Step 3: Compute (a+b)(c+d) = 46 x 134 = 6164
Step 4: Compute (3)-(2)-(1)=6164-2652-672 = 2840

Finally, multiply the output of step 1 by 10n, the output


of step 4 by 10n/2, and add them both with the output of
step 2.
6720000 + 284000 + 2652 = 7006652
Karatsuba Algorithm
To multiply X = 1234 and Y = 2345 using the Karatsuba
algorithm a b c d

X = 12 34 Y = 23 45
Steps:
Step 1: Compute a.c = 12 x 23 = 276
Step 2: Compute b.d = 34 x 45 = 1530
Step 3: Compute (a+b)(c+d) = 46 x 68 = 3128
Step 4: Compute (3) - (2) - (1) = 3128 - 1530 - 276 = 1322

Finally, multiply the output of step 1 by 10000 (104), the output of step 4

by 100 (102), and add them both with the output of step 2.
Result: 2760000 + 132200 + 1530 = 2893730
The product of 1234 and 2345 using the Karatsuba algorithm is 2897730.
Karatsuba Algorithm
compute the product of 23 and 67 using the Karatsuba
algorithm. a b c d

X = 2 3 Y = 6 7

Step 1: Compute a.c = 2 x 6 = 12


Step 2: Compute b.d = 3 x 7 = 21
Step 3: Compute (a+b)(c+d) = (2+3)(6+7) = 5 x 13 = 65
Step 4: Compute (3) - (2) - (1) = 65 - 21 - 12 = 32
Finally, multiply the output of Step 1 by 10^n (where n is the number of
digits), the output of Step 4 by 10^n/2, and add them both with the output
of Step 2.
1200 + 320 + 21 = 1541
product of 23 and 67 using the Karatsuba algorithm is 1541.
Karatsuba Algorithm

Time Complexity

Assuming that we replace two of the multiplications with


only one makes the program faster.

Karatsuba improves the multiplication process by replacing


the initial complexity from quadratic to

Where n is the number of digits of the numbers multiplying.

The Time Complexity of the algorithm can be represented as


follows
Karatsuba Algorithm

Time Complexity
Karatsuba Algorithm

Implementation

Algorithm
1. Compute starting set (a*c)
2. Compute set after starting set may it be ending set
(b*d)
3. Compute starting set with ending sets (a+b)(c+d)
4. Subtract values of Step 3 from Step 2 from Step 1
5. Add all the values with the following modifications:-
1. Pad up 10n to the number obtained from Step 1
2. Step 2 value unchanged
3. Pad up 10n/2 to the value obtained from Step 4.
Karatsuba Algorithm

Program

Sample IO
Karatsuba Multiplication Algorithm Test

Enter the first number: 1234


Enter the second number: 2345
Product: 2893730
Karatsuba Algorithm
import java.util.*; private static int getNumDigits(int x) {
public class Main { if (x == 0) {
public static int karatsuba(int x, int y) { return 1;
if (x < 10 || y < 10) { }
return x * y; int count = 0;
} while (x > 0) {
int m = Math.max(getNumDigits(x), count++;
getNumDigits(y)); x /= 10;
int halfM = m / 2; }
int powerOf10 = (int) Math.pow(10, halfM); return count;
int a = x / powerOf10; }
int b = x % powerOf10; public static void main(String[] args) {
int c = y / powerOf10; Scanner s1=new Scanner(System.in);
int d = y % powerOf10; System.out.print("enter number1-");
int ac = karatsuba(a, c); int x = s1.nextInt();
int bd = karatsuba(b, d); System.out.print("enter number2-");
int abcd = karatsuba(a + b, c + d); int y = s1.nextInt();
int result = ac * (int) Math.pow(10, 2 * halfM) + int product = karatsuba(x, y);
(abcd - ac - bd) * powerOf10 + bd; System.out.println(x + " * " + y + " = " + product);
return result; } }}
Karatsuba Algorithm
import java.math.BigInteger; BigInteger z0 = karatsuba(xLow, yLow);
import java.util.Scanner; BigInteger z1 =
public class KaratsubaAlgorithm { karatsuba(xLow.add(xHigh), yLow.add(yHigh));
public static BigInteger BigInteger z2 = karatsuba(xHigh,
karatsuba(BigInteger x, BigInteger y) { yHigh);
int n = Math.max(x.bitLength(), // Combine the results
y.bitLength()); BigInteger result = z2.shiftLeft(2 *
// Base case: if either x or y is half).add(z1.subtract(z2).subtract(z0).shiftLe
small, use standard multiplication ft(half)).add(z0);
if (n <= 2000) { return result;
return x.multiply(y); }
} public static void main(String[] args) {
// Split the numbers into two halves Scanner scanner = new
int half = (n + 32) / 64 * 32; // Scanner(System.in);
round up to the nearest multiple of 64 bits System.out.print("Enter the first
BigInteger mask = number: ");
BigInteger.ONE.shiftLeft(half).subtract(BigInt BigInteger x =
eger.ONE); scanner.nextBigInteger();
BigInteger xLow = x.and(mask); System.out.print("Enter the second
BigInteger yLow = y.and(mask); number: ");
BigInteger xHigh = x.shiftRight(half); BigInteger y =
BigInteger yHigh = y.shiftRight(half); scanner.nextBigInteger();
BigInteger product = karatsuba(x, y);
System.out.println("Product: " +
Interview questions

What is the Karatsuba algorithm?

The Karatsuba algorithm is a fast multiplication


algorithm that allows multiplying large integers in a
more efficient manner than the traditional multiplication
algorithm. It reduces the number of recursive
multiplications required by breaking down the numbers
into smaller parts.
Interview questions

How does the Karatsuba algorithm work?

The Karatsuba algorithm works by splitting the input


numbers into two halves and recursively calculating three
products. These three products are combined using some
arithmetic operations to obtain the final product.
Interview questions

What are the advantages of the Karatsuba algorithm over


traditional multiplication?

The Karatsuba algorithm has a lower time complexity than


traditional multiplication algorithms for large numbers.
It reduces the number of multiplications required and,
therefore, improves the overall efficiency of the
multiplication operation.
Interview questions

Can you explain the recursive steps involved in the


Karatsuba algorithm?

In the Karatsuba algorithm, the input numbers are


split into two halves. Three recursive
multiplications are performed: one for the lower
halves, one for the sum of the halves, and one for
the upper halves. These products are combined using
arithmetic operations to obtain the final result.
/ Ethnus /ethnus /
ethnuscodemith Codemithra code_mithra
ra

https://
learn.codemithra.com

[email protected] +91 7815 095 +91 9019 921


om 095 340

You might also like