Evaluate the square root of integer N (without database functions)

Source: Internet
Author: User

 
<Span style = "font-size: 14px;"> </span>
Today, I borrowed a high-performance programmer training book from the library, which is humorous. (In fact, I borrowed it when I saw that the author was the founder of stack overflow ). One example on dozens of pages is about square root. The code is very simple, but ''' is not understood for the first time ····Only then can we know that the principle is based on the Newton-laferson method, I .e. the Newton Iteration Method. For details, refer to Baidu.


 
<Span style = "font-size: 14px;"> private double squarebootappr (int n) {double r = n/2; while (ABS (R-N/R)> t) // T = 0.0000001 {r = 0.5 * (R + N/R);} return r ;}</span>

Another method is shift. The square root is used to calculate the square. If the square of the result is less than N. Less than N, the highest position is 1, and the opposite is 0 .(There is no first method with High Precision)

 
<Span style = "font-size: 14px;"> private double squarebootappr (int n) {int r = 0; For (INT I = 15; I> 0; I ++) {If (R + (1 <I) * (R + (1 <r) <n) {R + = 1 <I ;}} return r ;}</span>


Evaluate the square root of integer N (without library function)

Contact Us

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.