|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class provides an implementation of the Karatsuba multiplication algorithm. |
| 7 | + * |
| 8 | + * <p> |
| 9 | + * Karatsuba multiplication is a divide-and-conquer algorithm for multiplying two large |
| 10 | + * numbers. It is faster than the classical multiplication algorithm and reduces the |
| 11 | + * time complexity to O(n^1.585) by breaking the multiplication of two n-digit numbers |
| 12 | + * into three multiplications of n/2-digit numbers. |
| 13 | + * </p> |
| 14 | + * |
| 15 | + * <p> |
| 16 | + * The main idea of the Karatsuba algorithm is based on the following observation: |
| 17 | + * </p> |
| 18 | + * |
| 19 | + * <pre> |
| 20 | + * Let x and y be two numbers: |
| 21 | + * x = a * 10^m + b |
| 22 | + * y = c * 10^m + d |
| 23 | + * |
| 24 | + * Then, the product of x and y can be expressed as: |
| 25 | + * x * y = (a * c) * 10^(2*m) + ((a * d) + (b * c)) * 10^m + (b * d) |
| 26 | + * </pre> |
| 27 | + * |
| 28 | + * The Karatsuba algorithm calculates this more efficiently by reducing the number of |
| 29 | + * multiplications from four to three by using the identity: |
| 30 | + * |
| 31 | + * <pre> |
| 32 | + * (a + b)(c + d) = ac + ad + bc + bd |
| 33 | + * </pre> |
| 34 | + * |
| 35 | + * <p> |
| 36 | + * The recursion continues until the numbers are small enough to multiply directly using |
| 37 | + * the traditional method. |
| 38 | + * </p> |
| 39 | + */ |
| 40 | +public final class KaratsubaMultiplication { |
| 41 | + |
| 42 | + /** |
| 43 | + * Private constructor to hide the implicit public constructor |
| 44 | + */ |
| 45 | + private KaratsubaMultiplication() { |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Multiplies two large numbers using the Karatsuba algorithm. |
| 50 | + * |
| 51 | + * <p> |
| 52 | + * This method recursively splits the numbers into smaller parts until they are |
| 53 | + * small enough to be multiplied directly using the traditional method. |
| 54 | + * </p> |
| 55 | + * |
| 56 | + * @param x The first large number to be multiplied (BigInteger). |
| 57 | + * @param y The second large number to be multiplied (BigInteger). |
| 58 | + * @return The product of the two numbers (BigInteger). |
| 59 | + */ |
| 60 | + public static BigInteger karatsuba(BigInteger x, BigInteger y) { |
| 61 | + // Base case: when numbers are small enough, use direct multiplication |
| 62 | + // If the number is 4 bits or smaller, switch to the classical method |
| 63 | + if (x.bitLength() <= 4 || y.bitLength() <= 4) { |
| 64 | + return x.multiply(y); |
| 65 | + } |
| 66 | + |
| 67 | + // Find the maximum bit length of the two numbers |
| 68 | + int n = Math.max(x.bitLength(), y.bitLength()); |
| 69 | + |
| 70 | + // Split the numbers in the middle |
| 71 | + int m = n / 2; |
| 72 | + |
| 73 | + // High and low parts of the first number x (x = a * 10^m + b) |
| 74 | + BigInteger high1 = x.shiftRight(m); // a = x / 2^m (higher part) |
| 75 | + BigInteger low1 = x.subtract(high1.shiftLeft(m)); // b = x - a * 2^m (lower part) |
| 76 | + |
| 77 | + // High and low parts of the second number y (y = c * 10^m + d) |
| 78 | + BigInteger high2 = y.shiftRight(m); // c = y / 2^m (higher part) |
| 79 | + BigInteger low2 = y.subtract(high2.shiftLeft(m)); // d = y - c * 2^m (lower part) |
| 80 | + |
| 81 | + // Recursively calculate three products |
| 82 | + BigInteger z0 = karatsuba(low1, low2); // z0 = b * d (low1 * low2) |
| 83 | + BigInteger z1 = karatsuba(low1.add(high1), low2.add(high2)); // z1 = (a + b) * (c + d) |
| 84 | + BigInteger z2 = karatsuba(high1, high2); // z2 = a * c (high1 * high2) |
| 85 | + |
| 86 | + // Combine the results using Karatsuba's formula |
| 87 | + // z0 + ((z1 - z2 - z0) << m) + (z2 << 2m) |
| 88 | + return z2 |
| 89 | + .shiftLeft(2 * m) // z2 * 10^(2*m) |
| 90 | + .add(z1.subtract(z2).subtract(z0).shiftLeft(m)) // (z1 - z2 - z0) * 10^m |
| 91 | + .add(z0); // z0 |
| 92 | + } |
| 93 | +} |
0 commit comments