# Discussion on the biginteger. SQRT Method

Source: Internet
Author: User

We know that. NET Framework 4 already has the system. numerics. biginteger structure. However, this biginteger structure does not have the SQRT method. Let's write one by ourselves:

` 01:  Using System; 02:  Using System. numerics; 03:  04:  Namespace Skyiv 05: { 06:    Public static class  Extensions07: { 08:      Public static  Biginteger SQRT ( This  Biginteger X) 09: { 10:        If (X. Sign <0) Throw new  Argumentoutofrangeexception ( "X" , "Must be greater than or equal to zero" );11:        Biginteger Low, high; 12: Getlowandhigh (X, Out Low, Out High ); 13:        VaR Mid = low; 14:        VaR CMP = 0; 15:        While (Low. compareto (high) <= 0) 16: {17: Mid = (low + high)/2; 18: CMP = (mid * mid). compareto (X ); 19:          If (CMP <0) Low = Mid + 1; 20:          Else if (CMP> 0) High = Mid + (-1 ); 21:          Else return Mid; 22: } 23:        If (CMP> 0) Mid --;24:        Return Mid; 25: } 26:  27:      Static void Getlowandhigh ( Biginteger X, Out  Biginteger Low, Out  Biginteger High) 28: { 29:       VaR N = x. tobytearray (). length; 30:        If (N <2) 31: { 32: Low = 0; 33: High = X; 34:          Return ; 35: } 36:        VaR BS = New byte [N/2 + 1];37:        VaR K = BS. Length-2; 38:        If (N % 2 = 0) 39: { 40: BS [k] = 0x0b; 41: Low = New  Biginteger (BS ); 42: BS [k] = 0xb6; 43: High = New Biginteger (BS ); 44: } 45:        Else46: { 47: BS [k] = 0xb5; 48: Low = New  Biginteger (BS ); 49: BS [k] = 0x51; 50: BS [k + 1] = 0x0b;51: High = New  Biginteger (BS ); 52: } 53: } 54: } 55: }`

AboveProgramThe getlowandhigh method of rows 27th to 53 is used to obtain the lower limit and upper limit of the square root of a specified large integer. Then, the second-point search is used in the SQRT method of rows 8th to 25 to find the square root of the request.

The biginteger. tobytearray method converts biginteger to a byte array. For biginteger greater than zero, if the length of the converted byte array is fixed, the minimum value is in the format of 00-80-00-00-00, and the maximum value is in the form of 7f-ff-ff-ff-ff, it is exactly one smaller than the minimum value of the next group. See the following table:

N X SQRT (X)
2 00-80 0b
3 00-80-00 00-b5
4 00-80-00-00 0b-50
5 00-80-00-00-00 00-b5-04
6 00-80-00-00-00-00 0b-50-4f
7 00-80-00-00-00-00-00-00 00-b5-04-f3
8 00-80-00-00-00-00-00-00-00 0b-50-4f-33
9 00-80-00-00-00-00-00-00-00-00 00-b5-04-f3-33

As shown in the preceding table, if X is converted to a byte array and the length is N, the length of SQRT (X) is n/2 + 1 after being converted to a byte array.

If n is an even number (lines 40th to 43 in the program ):

• The minimum value is greater than 00-0b-00-00 -...
• The maximum value is less than 00-b6-00-00 -...

If n is an odd number (lines 47th to 51 in the program ):

• The minimum value is greater than 00-b5-00-00 -...
• The maximum value is less than 0b-51-00-00 -...

The. NET Framework 2.0 does not contain biginteger. I used to write biginteger in C # and implemented the SQRT method. See the following document:

• Timus 1153. supercomputer
• Let's talk about biginteger-using Fast Fourier Transformation
• Bigarithmetic-static class that provides arbitrary precision for arithmetic operations

When using Fast Fourier Transform to implement biginteger, It is a static method that calls the bigarithmetic class. The SQRT method calculates the square root using the Newton Iteration Method:

Ui + 1 = UI (3-vui2)/2

U∞ secondary convergence at 1/√V, Multiply by V to get √.V. The square root is faster than the binary search method in this article.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.