发电机组2的平方根位平方根、机组

2023-09-12 21:17:43 作者:半卷清词

我要产生的2的平方根为 300万位的数字。

I want to generate the digits of the square root of two to 3 million digits.

我知道牛顿迭代,但我不有多少知道如何使用C或C ++来实现它由于缺乏BigInteger的支持。有人能指出我朝着正确的方向?

I am aware of Newton-Raphson but I don't have much clue how to implement it in C or C++ due to lack of biginteger support. Can somebody point me in the right direction?

另外,如果有人知道如何做到这一点在Python(我是初学者),我也AP preciate吧。

Also, if anybody knows how to do it in python (I'm a beginner), I would also appreciate it.

推荐答案

您可以尝试使用映射:

A / B - > (A + 2B)/(A + B)开始 A = 1,B = 1 。这种收敛的sqrt(2)(实际上给出了连分数再$ P $它psentations)。

a/b -> (a+2b)/(a+b) starting with a= 1, b= 1. This converges to sqrt(2) (in fact gives the continued fraction representations of it).

现在的关键点:psented为矩阵乘法这可以重新$ P $(类似于斐波那契数)

Now the key point: This can be represented as a matrix multiplication (similar to fibonacci)

如果A_N和B_N是第n个号码中的步骤,然后

If a_n and b_n are the nth numbers in the steps then

[1 2] [A_N B_N] T = [A_第(n + 1)B_第(n + 1)] T [1 1]

[1 2] [a_n b_n]T = [a_(n+1) b_(n+1)]T [1 1]

现在让我们

[1〜2] N [A_1 B_1] T = [A_(N + 1)B_(N + 1)] T [1 1]

[1 2]n [a_1 b_1]T = [a_(n+1) b_(n+1)]T [1 1]

因此​​,如果2×2矩阵是,我们需要计算A N 可以通过重复平方来完成,只使用整数运算(所以你不必担心precision问题)。

Thus if the 2x2 matrix is A, we need to compute An which can be done by repeated squaring and only uses integer arithmetic (so you don't have to worry about precision issues).

另外请注意,在A / B,你得到的将永远是降低形式(GCD(A,B)= GCD(A + 2B,A + B)),所以如果你想使用一小部分类的再present中间结果,不要!

Also note that the a/b you get will always be in reduced form (as gcd(a,b) = gcd(a+2b, a+b)), so if you are thinking of using a fraction class to represent the intermediate results, don't!

由于第n分母就像是(1 + SQRT(2))^ N,拿到3万位,你可能会需要计算,直到3671656 日词。

Since the nth denominators is like (1+sqrt(2))^n, to get 3 million digits you would likely need to compute till the 3671656th term.

请注意,即使你正在寻找〜3.6万个长期,反复的平方可以让你计算第n项O(log n)的乘法和加法。

Note, even though you are looking for the ~3.6 millionth term, repeated squaring will allow you to compute the nth term in O(Log n) multiplications and additions.

此外,这可以很容易地平行,不像迭代的像牛顿迭代等。

Also, this can easily be made parallel, unlike the iterative ones like Newton-Raphson etc.