使用 BigInteger 进行 Karatsuba 乘法

2024-05-01

我首先使用 long 编写了 Karasuba 算法的代码。我认为它工作得很好。使用相同的逻辑,我将代码转换为 BigInteger,但由于某些原因,它给出了 StackOverflowError。

我不明白为什么。请帮忙。


EDIT1:长时间的代码也有一个逻辑缺陷。我不确定是什么。

EDIT2: long 的代码现在可以工作了。我错误地将“%”运算符换成了“/”。

EDIT3:现在一切都好。我将 .xor 更改为 .pow,将 == 更改为 .equals,并修复了 return 语句中的一些括号问题。谢谢大家的帮助!


这是正确的代码:

public static BigInteger karatsuba3(BigInteger i, BigInteger j){
    if (i.compareTo(Ten) == -1 || j.compareTo(Ten) == -1)
        return i.multiply(j);
    String length = getLength(i.max(j));
    BigInteger n = new BigInteger(length);
    if (n.mod(Two).equals(One))
        n = n.add(One);

    BigInteger a = i.divide(Ten.pow(n.divide(Two).intValue()));
    BigInteger b = i.mod(Ten.pow(n.divide(Two).intValue()));
    BigInteger c = j.divide(Ten.pow(n.divide(Two).intValue()));
    BigInteger d = j.mod(Ten.pow(n.divide(Two).intValue()));

    BigInteger first = karatsuba3(a,c);
    BigInteger second = karatsuba3(b,d);
    BigInteger third = karatsuba3(a.add(b),c.add(d));

    return ((first.multiply(Ten.pow(n.intValue()))).add ((((third.subtract(first)).subtract( second))).multiply(Ten.pow(n.divide((new BigInteger("2"))).intValue()))).add(second));
}

长代码 Karatsuba:

public static long karatsuba1(long i, long j){
    if (i < 10 || j < 10) 
        return i*j;
    double n = getLength(Math.max(i,j));
    if (n%2 == 1)
        n++;
    long a = (long) (i/Math.pow(10,(n/2)));
    long b = (long) (i%Math.pow(10,(n/2)));
    long c = (long) (j/Math.pow(10,(n/2)));
    long d = (long) (j%Math.pow(10,(n/2)));

    long first = karatsuba1(a, c);
    long second = karatsuba1(b, d);
    long third = karatsuba1(a + b, c + d);

    return ((long) ((first * Math.pow(10, n)) + ((third - first - second) * Math.pow(10, (n/2))) + second));
}

public static int getLength( long a){
    String b = Long.toString(a);
    return b.length();
}

Karasuba 与 BigInteger 代码:

public static BigInteger karatsuba3(BigInteger i, BigInteger j){
    BigInteger Ten = new BigInteger("10");
    if (i.compareTo(Ten) == -1 || j.compareTo(Ten) == -1)
        return i.multiply(j);
    String length = getLength(i.max(j));
    BigInteger n = new BigInteger(length);
    if (n.mod(new BigInteger("2")) == new BigInteger("1"))
        n.add(new BigInteger ("1"));

    BigInteger a = i.divide(Ten.xor(n.divide((new BigInteger("2")))));
    BigInteger b = i.mod(Ten.xor(n.divide((new BigInteger("2")))));
    BigInteger c = j.divide(Ten.xor(n.divide((new BigInteger("2")))));
    BigInteger d = j.mod(Ten.xor(n.divide((new BigInteger("2")))));

    BigInteger first = karatsuba3(a,c);
    BigInteger second = karatsuba3(b,d);
    BigInteger third = karatsuba3(a.add(b),c.add(d));

    return ((first.multiply(Ten.xor(n))).add (((third.subtract(first).subtract( second)))).multiply(Ten.xor(n.divide((new BigInteger("2"))))).add(second));
}

public static String getLength( BigInteger a){
    String b = a.toString();
    return Integer.toString(b.length());
}

在第一个片段中,这一行对我来说看起来是错误的:

long d = (long) (j/Math.pow(10,(n/2)));

所以现在你有c = d也许你想要:

long d = (long) (j%Math.pow(10,(n/2)));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 BigInteger 进行 Karatsuba 乘法 的相关文章

随机推荐

  • 在 Rest 中使用 POST 进行删除/更新?

    我明白 从接受的答案HTTP 和 REST 有什么区别 https stackoverflow com questions 2190836 what is the difference between http and rest that
  • 如何将Python列表分成不等长的子列表?

    我试图将用逗号分隔的元素列表划分为长度不等的块 我该如何划分它 list1 1 2 1 list2 1 1 1 1 1 1 1 2 1 1 1 3 1 1 1 4 list1 包含的元素是我希望将 list2 分成的块的大小 你可以结合以下
  • MySQL Select 查询 - 仅获取值的前 10 个字符

    好的 这就是问题所在 我有一个包含一些列的表 主题 是其中一列 无论 主题 字段包含一个包含 100 个字母的字符串 我都需要从 主题 字段获取前 10 个字母 例如 Table tbl 列 id subject value SQL查询 S
  • 如何在Go中从interface{}解组到interface{}

    我的系统中有多个通过 RPC 进行通信的节点 我正在尝试通过 RPC 将 map string interface 发送到另一个节点 发送方使用 json Marshal 接收方使用 json Unmarshal 来获取地图 假设在发送方
  • 以字符集安全的方式获取 Windows 上的进程列表

    这个帖子 https stackoverflow com questions 54686 how to get a list of current open windows process with java给出了一个在 Windows 下
  • Java HotSpot(TM) 64 位服务器虚拟机警告由于没有此类文件或目录而无法打开文件日志/gc.log

    当我尝试运行时出现此错误 RACK ENV test be rails test test system service provider map test rb seed 48088 Java HotSpot TM 64 Bit Serv
  • 适用于 Windows 的免费内存调试器? [复制]

    这个问题在这里已经有答案了 可能的重复 有 Windows 的良好 Valgrind 替代品吗 https stackoverflow com questions 413477 is there a good valgrind substi
  • 具有字典属性的 C# 匿名对象

    我正在尝试将字典转换为匿名类型 每个键都有一个属性 我尝试用谷歌搜索 但我所能找到的只是如何将匿名对象转换为字典 我的字典看起来像这样 var dict new Dictionary
  • 仅在天蓝色的仪表板中显示值?

    我有以下查询 AppMetrics where Name ReportImported summarize Value count 我想在仪表板中显示该值 它看起来像这样 是否有办法仅显示数字而不将其显示为带有 值 列的列表 您可以使用 A
  • 通话时是否可以播放音乐以便对方可以听到?安卓

    我正在尝试制作和应用程序打电话骗子 http phoneky com applications id y0y17763 最初为 Symbian 操作系统开发 是否可以在电话通话期间播放音乐 让接收方和呼叫方听到相同的声音或音乐 如果是 我该
  • PHP 内部的连接分解

    我看到一篇关于连接分解的文章 场景 1 不好 Select from tag Join tag post ON tag post tag id tag id Join post ON tag post post id post id Whe
  • Google Apps 脚本 Gmail CSV 导入工作表错误

    我从各种谷歌搜索中拼凑了这段代码 如果电子邮件有特定标签 这些代码将提取电子邮件的 CSV 附件 function importCSVFromGmail gets first latest message with set label va
  • Perl 使用什么哈希函数/算法?

    有人能解释一下 Perl 用于将字符串映射到索引的哈希函数 算法吗 有相关读物吗 这个答案早于 5 28 中进行的哈希函数更改 请参阅 默认哈希函数更改 perldelta 为 5 28 http perldoc perl org perl
  • 在应用程序版本中使用 svn 修订号

    在 VS2010 解决方案 不是 NET 中 我希望将 svn 修订号作为应用程序版本的一部分包含在内 我们目前不使用 makefile 仅使用 VS 解决方案 项目设置 我想在编译时获取工作副本修订号 将其存储到变量中 以便稍后在代码中使
  • 使用curl作为fgetcsv的fopen文件资源的替代品

    是否可以制作curl 访问url并将结果作为文件资源 就像 fopen 是如何做到的 我的目标 解析 CSV 文件 将其传递给 fgetcsv 我的障碍 fopen被禁用 我的代码块 在 fopen 中 url http download
  • ld:找不到 -llibtbb.dylib 的库

    我尝试从 opencv 2 4 8 apps haarfinder 编译一些文件 但出现以下错误 ld library not found for llibtbb dylib 注意双l在文件名中 我尝试按照这里的教程进行操作 http co
  • Microsoft EDGE 浏览器忽略企业模式列表

    由于 EDGE 似乎是 Windows 10 的 默认 浏览器 因此我们需要一种方法来 强制 EDGE 以 IE 模式打开我们的网站 或者至少引导用户在 IE 中打开网站 EDGE 似乎忽略了 X UA Compatible 元数据 而我们
  • 序列化的 lambda 且没有serialVersionUID?

    我正在尝试了解 Java 及其最新版本的序列化如何工作 我正在尝试像这样序列化 lambda Runnable r Runnable Serializable gt System out println This is a test 但我注
  • 使用 jaxb 编组时使用派生类

    我有一个具有公共基类的对象列表 我尝试使用 jaxb 将其序列化为 XML 我希望在编组时使用派生类的注释 但我在实现这一点时遇到了麻烦 import java util Arrays import java util List impor
  • 使用 BigInteger 进行 Karatsuba 乘法

    我首先使用 long 编写了 Karasuba 算法的代码 我认为它工作得很好 使用相同的逻辑 我将代码转换为 BigInteger 但由于某些原因 它给出了 StackOverflowError 我不明白为什么 请帮忙 EDIT1 长时间