Generating digits of square root of 2

后端 未结 9 1177
后悔当初
后悔当初 2020-12-29 08:36

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

I am aware of Newton-Raphson but I don\'t have much clue how to impleme

相关标签:
9条回答
  • 2020-12-29 09:24

    EDIT: I like this version better than the previous. It's a general solution that accepts both integers and decimal fractions; with n = 2 and precision = 100000, it takes about two minutes. Thanks to Paul McGuire for his suggestions & other suggestions welcome!

    def sqrt_list(n, precision):
        ndigits = []        # break n into list of digits
        n_int = int(n)
        n_fraction = n - n_int
    
        while n_int:                            # generate list of digits of integral part
            ndigits.append(n_int % 10)
            n_int /= 10
        if len(ndigits) % 2: ndigits.append(0)  # ndigits will be processed in groups of 2
    
        decimal_point_index = len(ndigits) / 2  # remember decimal point position
        while n_fraction:                       # insert digits from fractional part
            n_fraction *= 10
            ndigits.insert(0, int(n_fraction))
            n_fraction -= int(n_fraction)
        if len(ndigits) % 2: ndigits.insert(0, 0)  # ndigits will be processed in groups of 2
    
        rootlist = []
        root = carry = 0                        # the algorithm
        while root == 0 or (len(rootlist) < precision and (ndigits or carry != 0)):
            carry = carry * 100
            if ndigits: carry += ndigits.pop() * 10 + ndigits.pop()
            x = 9
            while (20 * root + x) * x > carry:
                    x -= 1
            carry -= (20 * root + x) * x
            root = root * 10 + x
            rootlist.append(x)
        return rootlist, decimal_point_index
    
    0 讨论(0)
  • 2020-12-29 09:24

    As for arbitrary big numbers you could have a look at The GNU Multiple Precision Arithmetic Library (for C/C++).

    0 讨论(0)
  • 2020-12-29 09:24

    Here is a short version for calculating the square root of an integer a to digits of precision. It works by finding the integer square root of a after multiplying by 10 raised to the 2 x digits.

    def sqroot(a, digits):
        a = a * (10**(2*digits))
        x_prev = 0
        x_next = 1 * (10**digits)
        while x_prev != x_next:
            x_prev = x_next
            x_next = (x_prev + (a // x_prev)) >> 1
        return x_next
    

    Just a few caveats.

    You'll need to convert the result to a string and add the decimal point at the correct location (if you want the decimal point printed).

    Converting a very large integer to a string isn't very fast.

    Dividing very large integers isn't very fast (in Python) either.

    Depending on the performance of your system, it may take an hour or longer to calculate the square root of 2 to 3 million decimal places.

    I haven't proven the loop will always terminate. It may oscillate between two values differing in the last digit. Or it may not.

    0 讨论(0)
提交回复
热议问题