📜  实现两个数相乘的 Schonhage-Strassen 算法的Java程序

📅  最后修改于: 2022-05-13 01:55:44.370000             🧑  作者: Mango

实现两个数相乘的 Schonhage-Strassen 算法的Java程序

Schonhage-Strassen 算法 是乘以非常大的整数值(30000 到 150000 十进制数字)的最快方法之一。该算法由Arnold SchönhageVolker Strassen开发。尽管 Furer算法比 Schonhage-Strassen 算法快,但它没有实际应用,除了银河算法(可能不用于地球上的任何数据)。因此,Schonhage-Strassen 算法被认为是乘法大整数值的最佳算法。

在这种乘法方法中,首先将两个整数相乘而不执行进位。然后得到的结果集称为Acyclic ConvolutionLinear Convolution 。然后我们执行线性卷积的各个列。

例子:


Java
// Java program to implement Schonhage-Strassen's
// Multiplication Algorithm
import java.io.*;
 
class GFG {
    // two class level variables to
    // store the given values
    static long a, b;
    // class level array to store the LinearConvolution
    static int[] linearConvolution;
    // an integer variable to determine
    // the length of the LinearConvolution
    static int length;
 
    // to count the no.of digits in each of
    // the given values a and b
    static int countDigits(long num)
    {
        // an integer variable initialized to
        // 0 to store the no.of digits
        int count = 0;
        // as long as the number is
        // greater than 0, divide it by 10
        // and increment the count
        while (num > 0) {
            num /= 10;
            count++;
        }
        // return the count when number becomes 0
        return count;
    }
 
    // to perform schonhage-Strassen's Multiplication
    static void schonhageStrassenMultiplication()
    {
        // first find the LinearConvolution
        findLinearConvolution();
        // Then perform carry on it
        performCarry();
    }
 
    // to find LinearConvolution
    static void findLinearConvolution()
    {
        // no.of digits in first number (a)
        int aDigits = countDigits(a);
        // no.of digits in second number (b)
        int bDigits = countDigits(b);
        // a temporary variable to store the value of a
        long temp = a;
        // length of the LinearConvolution is
        // 1 less than the no.of Digits in a +
        // no.of digits in b
        length = aDigits + bDigits - 1;
        linearConvolution = new int[length];
        for (int i = 0; i < aDigits; i++, b /= 10) {
            a = temp;
            for (int j = 0; j < bDigits; j++, a /= 10) {
                // multiply the current digit of a with
                // current digit of b and store in
                // LinearConvolution
                linearConvolution[i + j]
                    += (b % 10) * (a % 10);
            }
        }
        System.out.print("The Linear Convolution is: [ ");
        for (int i = length - 1; i >= 0; i--) {
            // print the LinearConvolution array
            System.out.print(linearConvolution[i] + "  ");
        }
        System.out.println("]");
    }
 
    // to perform carry on the obtained LinearConvolution
    static void performCarry()
    {
        // initialize product to 0
        long product = 0;
        int carry = 0, base = 1;
        // for every value in the LinearConvolution
        for (int i = 0; i < length; i++) {
            linearConvolution[i] += carry;
            // add the product of base and units digit of
            // LinearConvolution[i] to the product
            product
                = product
                  + (base * (linearConvolution[i] % 10));
            // now LinearConvolution[i]/10
            // will become the carry
            carry = linearConvolution[i] / 10;
            base *= 10;
        }
        System.out.println("\nThe Product is : " + product);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        // initialize the two declared class variables with
        // the desired values
        a = 2604;
        b = 1812;
        // call schonhageStrassenMultiplication() method
        schonhageStrassenMultiplication();
    }
}



输出
The Linear Convolution is: [ 2  22  50  14  44  4  8  ]

The Product is : 4718448

时间复杂度: O(n.logn.loglogn)