You are on page 1of 15

Karatsuba Algorithm

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Introduction

 The Karatsuba algorithm was the first multiplication algorithm


asymptotically faster than the quadratic “grade school” algorithm.
• It reduces the multiplication of two n-digit numbers to at most 3
n log 2 3 approx 3 n 1.585 single-digit multiplications in general.

Why the Karatsuba algorithm?


• The goal of the algorithm is designed because the design space is
surprisingly rich. Its time complexity is follows and do not it as
time complexity 
O(n^log2(3)) time (~ O(n^1.585))
Where n is the number of digits of the numbers multiplying. It is
discussed by multiplying two big integer numbers to show internal
working step by step. The goal is to reduce the space complexity for which
the integer numbers terms will be broken down in such a way to x and y
are broken into a set of digits as the logic behind it is divide and conquer.
If the numbers are smaller there is no need to multiply, standard
mutilation of two integers is preferred.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Algorithm Steps

 Compute starting set  (a*c)


 Compute set after starting set may it be ending set (b*d)
 Compute starting set with ending sets
 Subtract values of step 3 from step2 from step1
 Pad up (Add) 4 zeros to the number obtained from Step1, step2
value unchanged, and pad up two zeros to value obtained from
step4.
Illustration:
Input: x = 1234, y = 5678

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Processing: As per above inputs
 
x = 1234
y = 5678
 
a = 12, b = 34
c = 56, d = 78
 
Step 1: a * c = 672
Step 2: b * d = 2652
Step 3: (a+b)(c+d) = 134 * 46 = 6164
Step 4: 6164 - 2652 - 672 = 2840
Step 5: 6720000 + 2652 + 284000 = 7006652

Output: 7006652

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program

int noOneLength = numLength(x);

int noTwoLength = numLength(y);


= Math.max(noOneLength, noTwoLength);

Integer halfMaxNumLength
= (maxNumLength / 2) + (maxNumLength % 2);

long maxNumLengthTen
= (long)Math.pow(10, halfMaxNumLength);

long a = x / maxNumLengthTen;
long b = x % maxNumLengthTen;

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
public static void main(String[] args)
long c = y / maxNumLengthTen;
{
long d = y % maxNumLengthTen;
long expectedProduct = 1234 * 5678;
long actualProduct = mult(1234, 5678);
long z0 = mult(a, c);
long z1 = mult(a + b, c + d);
System.out.println("Expected 1 : " +
long z2 = mult(b, d); expectedProduct);
long ans = (z0 * (long)Math.pow(10,
halfMaxNumLength * 2) +
System.out.println("Actual 1 : " +
((z1 - z0 - z2) * (long)Math.pow(10, actualProduct + "\n\n");
halfMaxNumLength) + z2));
assert(expectedProduct == actualProduct);
return ans;
expectedProduct = 102 * 313;
}
actualProduct = mult(102, 313);

public static int numLength(long n)


System.out.println("Expected 2 : " +
{ expectedProduct);
int noLen = 0;
while (n > 0) { System.out.println("Actual 2 : " + actualProduct
noLen++; + "\n\n");
n /= 10;
}
return noLen;
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
  assert(expectedProduct == actualProduct); actualProduct = mult(x, y);
expectedProduct = 1345 * 63456; // Again printing the expected
actualProduct = mult(1345, 63456); and
System.out.println("Expected 3 : " + // corresponding actual product
expectedProduct);
System.out.println("Expected: "
System.out.println("Actual 3 : " +
actualProduct + "\n\n"); + expectedProduct);
assert(expectedProduct == System.out.println("Actual: " +
actualProduct); actualProduct + "\n\n");
Integer x = null; assert(expectedProduct ==
Integer y = null; actualProduct);
Integer MAX_VALUE = 10000; }
Random r = new Random(); }
for (int i = 0; i < MAX_VALUE; i++) { }
x = (int) r.nextInt(MAX_VALUE);
y = (int) r.nextInt(MAX_VALUE);
expectedProduct = x * y;
Output
if (i == 9999) {
// Prove assertions catch the bad stuff.
Product 1 : 85348320
expectedProduct = 1;
Product 2 : 21726
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Longest Sequence of 1 after flipping a
bit

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Longest Sequence

• The longest subsequence is common to all the given


sequences is referred to as Longest Common Subsequence.

• The reason for using the LCS is to restrict the element of the
subsequences from occupying the consecutive position within
the original sequences.

Examples
Input : 1775
Output : 8

Binary representation of 1775 is 11011101111.After flipping the


highlighted bit, we get consecutive 8 bits.

© 2016 SMART Training Resources Pvt. Ltd.


Long Sequence-Example Program
class GFG maxLen = Math.max(prevLen + currLen,
{ maxLen);
static int flipBit(int a) a >>= 1;
{ }
if (~a == 0)
{ return maxLen + 1;
return 8 * sizeof(); }
} static byte sizeof()
int currLen = 0, prevLen = 0, maxLen = 0; {
while (a != 0) byte sizeOfInteger = 8;
{ return sizeOfInteger;
if ((a & 1) == 1) }
{
currLen++; public static void main(String[] args)
} {
else if ((a & 1) == 0) System.out.println(flipBit(13));
{ System.out.println(flipBit(1775));
prevLen = (a & 2) == 0 ? 0 : currLen; System.out.println(flipBit(15));
currLen = 0; }
} }

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Long Sequence-Example Program

Output :
4
8
5

© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Swap two nibbles in a byte

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Swap two nibbles in a byte

 A nibble is a four-bit aggregation, or half an octet.

 There are two nibbles in a byte. 

 Given a byte, swap the two nibbles in it.

 To swap the nibbles, we can use bitwise &, bitwise ” operators.

 A byte can be represented using a unsigned char in C as size of char is


1 byte in a typical C compiler. 

 For example 100 is be represented as 01100100 in a byte (or 8 bits).


The two nibbles are (0110) and (0100). If we swap the two nibbles, we
get 01000110 which is 70 in decimal.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program

class Main{
public static void main(String arg[]) { Output
70
int x = 100;
int ans= (x & 0x0F) << 4 | (x & 0xF0) >> 4; //0x0F=15; 0xF0=240;

System.out.print(ans);
}
}

0x0F – Hexadecimal value of 15


0xF0 – Hexadecimal value of 240

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank You …

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.

You might also like