求一个大数的立方根

发布于 2024-10-06 14:29:03 字数 43 浏览 3 评论 0原文

我需要找到一个向上舍入的巨大(5k 位左右)数字的立方根。 我该怎么做?

I need to find the cube root of a huge(5k bits or so) number rounded upwards.
How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-10-13 14:29:03

如果 GNU bc 适合您,则可能会这样做:

http://phodd.net/gnu-bc/bcfaq。 html#bccbrt

编辑:

它本质上归结为:

$ bc -l
define cbrt(x) { return e(l(x)/3) }

您需要增加比例变量以获得必要的精度:

$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

define cbrt(x) { return e(l(x)/3) }

cbrt(10000000000000000000000000000000000000000000000000000000000000000000)^3
9999999999999999999845725361475980907263179272258247094885777761435.\
89049462743995306310

scale=1000

cbrt(10000000000000000000000000000000000000000000000000000000000000000000)^3
9999999999999999999999999999999999999999999999999999999999999999999.\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999978254573198390239858\
069738839057154871628814670160708326688382280410

正如您可能注意到的那样,不增加比例变量(在我的系统上默认为 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:

$ bc -l
define cbrt(x) { return e(l(x)/3) }

You will need to increase the scale variable in order to have the necessary precision:

$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

define cbrt(x) { return e(l(x)/3) }

cbrt(10000000000000000000000000000000000000000000000000000000000000000000)^3
9999999999999999999845725361475980907263179272258247094885777761435.\
89049462743995306310

scale=1000

cbrt(10000000000000000000000000000000000000000000000000000000000000000000)^3
9999999999999999999999999999999999999999999999999999999999999999999.\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999999999999999999999999\
99999999999999999999999999999999999999999999999978254573198390239858\
069738839057154871628814670160708326688382280410

As you probably noticed, without increasing the scale variable (on my system it defaults to 20) the result has nowhere near your required precision.

柠檬 2024-10-13 14:29:03

这是一个简单的迭代算法。请注意,他们特意指出了平方根的特殊情况

一个特殊的例子是我们所熟悉的
平方根算法。通过设置 n =
2、第2步的迭代规则
变为平方根迭代规则

相同的技术可以应用于立方根:设置 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:

A special case is the familiar
square-root algorithm. By setting n =
2, the iteration rule in step 2
becomes the square root iteration rule

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文