Find 10,000 digits after the decimal point of the root number 2, random thoughts on byte interview questions

origin

Yesterday in the group, a small partner sent an interview question about the byte backend, the question stem is to ask for 2 \sqrt{2}2 10,000 digits after the decimal point. After some intuitive thinking and searching information on the Internet, there are roughly three solutions as follows. Since the decimal precision in many languages ​​​​can’t reach the level of 10 (-10000), let’s simplify this question to 6 digits after the decimal point .

Method 1, intuitive dichotomy

Assuming that the given number is n , you can set the left boundary to 0 and the right boundary to n, and take mid = (0 + n) / 2. If mid 2 is less than n, set the left boundary to mid, otherwise the boundary is mid. The condition for terminating the cycle is that the difference between the left and right boundaries is less than the precision threshold (right - left < threshold), the simple code is as follows:

double getSqrt(int n, double threshold)
	double left = 0, right = n;
	while ( right - left > threshold){
    
    
		double mid = (left + right) / 2;
		if (mid * mid == n) return mid;
		else if (mid * mid < n) left = mid;
		else right = mid; 
	} 
	return left;

The logic is relatively simple, so I won’t make too many comments

Method 2, Newton iteration method

The Newton iterative method can theoretically find the roots of most polynomials (the minimum requirement is that the function is second-order derivable, which is not the scope of this article, so it is skipped), the general idea is as follows: given the curve equation f(x), the root of the equation is required
to be xn x_nxnMake a tangent at the place, find xn + 1 x_{n+1}xn+1, where xn x_nxnIt is a certain solution in the iterative process. The initial solution can be set arbitrarily, but it is better to be as accurate as possible. It is easy to get xn x_n
by derivationxnThe equation of the tangent line is fff( x n x_n xn) + f ′ f' f( x n x_n xn)( x x x- x n x_n xn),
the simplification can be calculated as xn + 1 = xn − f ( xn ) f ′ ( xn ) {x_{n+1} = x_n - \frac {f(x_n)} {f'(x_n)}}xn+1=xnf(xn)f(xn)
Then you can iterate this process continuously, and the termination condition is xn − xn + 1 < threshold {x_n} - {x_{n+1}} < thresholdxnxn+1<t h r e s h o l dFor
this question,f ( x ) = x 2 − nf(x) = x^2 - nf(x)=x2n , the correspondingf ′ ( x ) = 2 x f'(x) = 2xf(x)=2 x , so the code is as follows

double getSqrtNt(double n, double threshold){
	doulbe x_n1 = n / 2;  //起始点设置, 这是我随便写的一个, 可以有更好的方法
	double x_n = n;
	while (abs(x_n1 - x_n) > threshold){
		x_n = x_n1;
		x_n1 = xn - (xn * xn - n)/(2 * xn)
	}
	return xn;
}

Method 3, continued fraction method, suitable for hand calculation

In fact, this method is essentially the same as the Newton iteration method. This method can be applied to students who are not very proficient in derivatives, and the conditions of this method are relatively harsh. There are two points

  1. square root only
  2. The radicand is a positive integer.
    The steps are as follows, requiring s \sqrt{s}s , first decompose it into s = a 2 + bs = a^2 + bs=a2+b , wherea 2 >> ba^2 >> ba2>>b , then according to the required precision
    s = a + b 2 a + b 2 a + . . . \sqrt{s} = a + \frac {b} {2a + \frac {b} {2a + ...}}s =a+2a + _2 a + . . .bb
    For example, to find the square root of 150, first find 150 = 12 ∗ 12 + 6 150 = 12 * 12 + 6150=1212+6
  • Find the first-order situation, 150 = 12 \sqrt{150} = 12150 =12
  • For the second order, 150 = 12 + 6 / 24 = 12.25 \sqrt{150} = 12 + 6/24 = 12.25150 =12+6/24=1 2 . 2 5 , the accuracy is higher now
  • Third order, 150 = 12 + 6 / ( 24 + ( 6 / 24 ) ) = 12.2474 \sqrt{150} = 12 + 6/(24 + (6/24)) = 12.2474150 =12+6/(24+(6/24))=1 2 . 2 4 7 4 , a little higher
  • …the deeper the precision, the higher the
    accuracy Query the calculator's 150 \sqrt{150}150 The value of is 12.24744, which is very close to our answer.
    The method of proof is also very simple. I will post the relevant proof of Mr. Li Yongle later.

References

[1] How to explain Newton's iterative method to find the square root in an easy-to-understand manner? Numerical Analysis? - Student Ma's answer - Zhihu
[2] How to calculate the square root by hand? After learning this, you can show off to your friends again!

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/118873223