- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在实现 Karatsuba 算法时遇到了一些问题。我的项目限制我使用以下库:iostream、iomanip、cctype、cstring。此外,我仅限于仅使用整数内置类型和数组/动态数组来处理数字(仅输入无符号整数)。我已经构建了一个类来使用动态数组处理任意大小的整数。我需要实现一个乘以大整数的函数,如果可能的话我想使用 Karatsuba。我遇到的问题是如何分解大整数并进行算法中要求的乘法运算。我认为这应该递归完成。我希望有人能给我一个如何做到这一点的例子。
例如:
我有两个数字存储在动态数组中。假设它们是:
X = 123456789123456789123456789Y = 987654321987654321987654321987654321
考虑到 unsigned int 类型的存储限制,Karatsuba 需要如何处理这个问题?任何帮助将不胜感激!
最佳答案
如果您查看伪代码 here ,您可以稍微修改它以与这样的数组一起使用:
procedure karatsuba(num1, num2)
if (num1.Length < 2) or (num2.Length < 2) //Length < 2 means number < 10
return num1 * num2 //Might require another mult routine that multiplies the arrays by single digits
/* calculates the size of the numbers */
m = max(ceiling(num1.Length / 2), ceiling(num2.Length / 2))
low1, low2 = lower half of num1, num2
high1, high2 = higher half of num1, num2
/* 3 calls made to numbers approximately half the size */
z0 = karatsuba(low1,low2)
z1 = karatsuba((low1+high1),(low2+high2))
z2 = karatsuba(high1,high2)
//Note: In general x * 10 ^ y in this case is simply a left-shift
// of the digits in the 'x' array y-places. i.e. 4 * 10 ^ 3
// results in the array x[4] = { 4, 0, 0, 0 }
return (z2.shiftLeft(m*2)) + ((z1-z2-z0).shiftLeft(m)) + (z0)
如果您为数字数组定义了加法、减法和额外的个位数乘法例程,则该算法应该很容易实现(当然还有其他所需的例程,例如数字移位和数组拆分)。
因此,对于那些其他例程还有其他初步工作,但这就是 Karatsuba 例程的实现方式。
关于c++ - Karatsuba C++ 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19841186/
当我运行程序时,它因段错误而崩溃。另外,当我在 codeblocks IDE 中调试代码时,我也无法调试它。程序甚至在调试开始之前就崩溃了。我无法理解这个问题。任何帮助,将不胜感激。谢谢!! #inc
我只是简单地遵循了 wiki 上的伪代码 http://en.wikipedia.org/wiki/Karatsuba_algorithm但是这个实现的结果很不稳定。它有时会起作用,但以防 100*1
我一直在学习Karatsuba's algorithm on Wikipedia我在这个让我困惑的部分停了下来。为什么这个算法会溢出,我不明白他为解决这个问题所做的步骤。这是我的问题的截图 最佳答案
只是想知道为什么选择 Karatsuba 乘法的基本情况(此处显示:http://www.sanfoundry.com/java-program-karatsuba-multiplication-al
我最近实现了 Karatsuba 乘法作为个人练习。我在 pseudocode provided on wikipedia 之后用 Python 编写了我的实现: procedure karatsub
嗨,我正在尝试用 Javascript 实现 karatsuba 算法。到目前为止,该算法在某些情况下工作正常,例如当整数的长度为 4 或 8 时。当整数的长度为 6 时,它会打印出错误的结果 例如:
我正在实现 Karatsuba multiplication在 Scala(我的选择)中学习在线类(class)。考虑到该算法是为了乘以大数,我选择了由 Java 支持的 BigInt 类型 BigI
我写了这个按位 Karatsuba 乘法算法。它不使用字符串或 math.pow。就是分而治之递归,按位运算和加法: def karatsuba(x,y): n = max(x.bit_lengt
我正在尝试实现递归 Karatsuba 算法以乘以两个多项式(相同次数)。我的代码仍然不适用于次数大于 1 的多项式。任何人都可以向我解释我在我的函数中做错了什么吗?它应该适用于偶数和奇数系数。 多项
我对编程还比较陌生,我不希望使用这种算法在运行时间方面特别高效,而只是尝试复制 Karatsuba 算法并使其发挥作用。 我试过很多数字和小数字(比如 y = 40004009343254,x = 4
我在 python 中实现 karatsuba 算法时遇到了问题。我正在使用 base 2 中的列表(MSB 在列表的末尾)。给我的实现是这样的: Input: 2 bit-numbers of bi
我正在编写自己的 karatsuba algh 变体,但无法使其与真正的大整数一起使用 'use strict'; const karatsuba = function (x, y) { if (
我在实现 Karatsuba 算法时遇到了一些问题。我的项目限制我使用以下库:iostream、iomanip、cctype、cstring。此外,我仅限于仅使用整数内置类型和数组/动态数组来处理数字
这里是初学者。我花了一天的大部分时间研究 Karatsuba 算法,只是因为我认为它会富有成果。我在这里看到过类似的问题,但它们是用其他语言写的,而且看起来异常复杂。以下是我的代码。一旦它遇到对 ac
我正在尝试实现 Karatsuba multiplication通过递归调用。下面的代码应该可以工作,但我总是得到错误的答案。有什么想法吗? public static long karats
这是我对 karatsuba 算法的实现,它在删除部分被注释时运行良好,但是当我将其插入代码时,它会导致输出错误! (例如对于测试 n=5,a=b={1, 2, 3, 4, 5}) void conv
我看到了维基页面:https://en.wikipedia.org/wiki/Karatsuba_algorithm karatsuba 算法的递归关系为: T(n) = 3T(n/2) + cn +
今天,我听说了 Karatsuba 算法,一种快速乘法算法。我很好奇这个“快”是什么意思? 通常,我们在计算一段代码的时间复杂度时,会考虑使用 * 运算符作为 O(1) 的乘法运算,如果它始终为真,那
我已经实现了两个字节数组的乘法运算,它运行良好。更准确地说,我需要将一个 64 字节的操作数与一个 32 字节的操作数相乘。 我以最简单的方式实现了它:我做了一个双循环,然后为每个数组中的每个字节计算
我正在尝试使用 karatsuba 技术实现大数乘法。我陷入了无法解决的段错误。该程序似乎总是在 3 或 4 级递归后退出。 sizeof(bint) 返回 20000。我的 bint 由一组短裤组成
我是一名优秀的程序员,十分优秀!