编程语言
首页 > 编程语言> > 大数乘法 - Karatsuba算法

大数乘法 - Karatsuba算法

作者:互联网

参考:https://blog.csdn.net/u010983881/article/details/77503519

/**
 * Karatsuba乘法
 */
public static long karatsuba(long num1, long num2){
    //递归终止条件
    if(num1 < 10 || num2 < 10) return num1 * num2;

    // 计算拆分长度
    int size1 = String.valueOf(num1).length();
    int size2 = String.valueOf(num2).length();
    int halfN = Math.max(size1, size2) / 2;

    /* 拆分为a, b, c, d */
    long a = Long.valueOf(String.valueOf(num1).substring(0, size1 - halfN));
    long b = Long.valueOf(String.valueOf(num1).substring(size1 - halfN));
    long c = Long.valueOf(String.valueOf(num2).substring(0, size2 - halfN));
    long d = Long.valueOf(String.valueOf(num2).substring(size2 - halfN));

    // 计算z2, z0, z1, 此处的乘法使用递归
    long z2 = karatsuba(a, c);
    long z0 = karatsuba(b, d);
    long z1 = karatsuba((a + b), (c + d)) - z0 - z2;

    return (long)(z2 * Math.pow(10, (2*halfN)) + z1 * Math.pow(10, halfN) + z0);
}

 

ccluqh 发布了241 篇原创文章 · 获赞 6 · 访问量 7209 私信 关注

标签:num1,num2,大数,valueOf,long,String,halfN,Karatsuba,乘法
来源: https://blog.csdn.net/qq_28468707/article/details/104524454