求一个大数的立方根
我需要找到一个向上舍入的巨大(5k 位左右)数字的立方根。 我该怎么做?
I need to find the cube root of a huge(5k bits or so) number rounded upwards.
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 GNU bc 适合您,则可能会这样做:
http://phodd.net/gnu-bc/bcfaq。 html#bccbrt
编辑:
它本质上归结为:
您需要增加比例变量以获得必要的精度:
正如您可能注意到的那样,不增加比例变量(在我的系统上默认为 20)结果远未达到您所需的精度。
If GNU bc is fine for you, this might do:
http://phodd.net/gnu-bc/bcfaq.html#bccbrt
EDIT:
It essentially boils down to:
You will need to increase the scale variable in order to have the necessary precision:
As you probably noticed, without increasing the scale variable (on my system it defaults to 20) the result has nowhere near your required precision.
这是一个简单的迭代算法。请注意,他们特意指出了平方根的特殊情况:
相同的技术可以应用于立方根:设置 n = 3 并迭代,直到达到所需的
对于注释中的规范“需要四舍五入到最接近的整数并且精确”的情况,这仅适用于具有整数或有理立方根的数字。也就是说,您可以使用引用的算法通过迭代找到此精度级别的答案,直到一次迭代的结果与下一次迭代的结果之间的差异小于 0.5。这足够接近以确保未来的迭代不会偏离该近似值太远。
这是数值分析课的练习吗?如果是这样,我怀疑这正是以这种方式提出问题的原因:讲师希望您将一般规则应用于特定问题。
Here is a straightforward iterative algorithm. Note that they make a point of calling out the special case of square roots:
The same technique can be applied to cube roots: set n = 3 and iterate until you achieve the desired precision.
In the case of the specification in the comment "it needs to be rounded up to closest integer and be exact", that will only be possible for numbers that have integer or rational cube roots. That said, you can use the cited algorithm to find an answer to this level of precision by iterating until the difference between the result of one iteration and the next is less than 0.5. That is close enough to assure that future iterations won't wander far from that approximation.
Is this an exercise for a numerical analysis class? If so, I suspect this is exactly why the question was posed this way: the instructor would like you to apply the general rule to the specific problem.