Karatsuba算法实现:适用于较小的ns,中断适用于较大的ns

时间:2019-03-20 16:58:48

标签: java string multiplication karatsuba

我正在研究Karatsuba算法的数字乘法,但是与大多数使用字符串作为主要数据结构而不是BigNumbers或longs的实现不同。我写了一个递归的解决方案来解决似乎对所有n <6都有效的问题,但是由于某些原因,尽管所有基本情况都可行,但对于大于6的奇数ns却无法工作。这是程序的karatsuba部分,调试中还留下了一些印刷品。本文中使用的所有方法均应按预期工作,我已对其进行了全面测试。对于值factor1 =“ 180”和factor2 =“ 109”,将输出正确的结果。对于值factor1 =“ 1111”和factor2 =“ 1111”,将输出正确的结果。对于factor1 =“ 2348711”和factor2 =“ 8579294”,程序在应输出“ 20150282190034”时输出“ 20358060808034”。我尝试回溯逻辑,但找不到确切出问题的地方。如果任何人对某些地方可能无法正常工作有任何见识,将不胜感激。

public static String multiply(String factor1, String factor2) {
    // base case of length = 1
    System.out.println("Factor1 " + factor1 + " factor2 " + factor2);
    if (factor1.length() == 1 && factor2.length() == 1) {
        return smallNumberMultiplication(factor1, factor2);
    } else if (factor1.length() == 1 && factor2.length() == 2) { //these conditions needed for odd-size #s
        return smallNumberMultiplication(factor1, factor2); // max iteration = 10
    } else if (factor1.length() == 2 && factor2.length() == 1) {
        return smallNumberMultiplication(factor2, factor1); // max iteration = 10
    }

    // check which factor is smaller, find the index at which the value is split
    int numberLength = factor1.length();
    int middleIndex = numberLength / 2;
    // Find the power to which 10 is raised such that it follows Karatsuba's algorithm for ac
    int powerValue = numberLength + numberLength % 2;

    // divide both numbers into two parts bounded by middleIndex place
    String[] tempSplitString = splitString(factor1, middleIndex);
    String f1Large = tempSplitString[0], f1Small = tempSplitString[1];
    tempSplitString = splitString(factor2, middleIndex);
    String f2Large = tempSplitString[0], f2Small = tempSplitString[1];

    String multiplyHighestNumbers, multiplySmallestNumbers, multiplyMiddleNumbers;
    // large factor1 * large factor2
    multiplyHighestNumbers = multiply(f1Large, f2Large);
    // Multiply (f1Large + f1Small)*(f2Large + f2Small)
    multiplyMiddleNumbers = multiply(addTwoValues(f1Large, f1Small), addTwoValues(f2Large, f2Small));
    // small factor1 * small factor2
    multiplySmallestNumbers = multiply(f1Small, f2Small);

    // add trailing zeros to values (multiply by 10^powerValue)
    String finalHighestNumber = addTrailingZeros(multiplyHighestNumbers, powerValue);
    String finalMiddleNumber = addTrailingZeros(
            subtractTwoValues(subtractTwoValues(multiplyMiddleNumbers, multiplyHighestNumbers),
                    multiplySmallestNumbers),
            powerValue / 2);
    String finalSmallestNumber = multiplySmallestNumbers;

    // add each part together
    return removeLeadingZeros(addTwoValues(addTwoValues(finalHighestNumber, finalMiddleNumber), finalSmallestNumber));
}

1 个答案:

答案 0 :(得分:1)

我注意到了两个问题:

  • 使用不同的值进行分割(middleIndex)和移位(powerValue)(通过在零上添加来不必要地实现)。
    为了使productHighParts(“ multiplyHighestNumbers”)的长度更接近其他产品,请使用(factor1.length() + factor2.length()) / 4(两个因子的平均长度的一半)。
  • 此长度必须是 splitString()中次要部分的长度,而不是前导部分的长度。

(请注意,前两个受控语句可以组合:
if (factor1.length() <= 1 && factor2.length() <= 2)。)