Calculation of the square root of an integer (2)

Source: Internet
Author: User

Abstract:

This article provides another program for calculating the square root of an integer, which is different from the calculation of the square root of an integer (1). It mainly uses the Newton method to calculate the square root. The big Integer Operation calls the GMP integer library. All code can be downloaded from the http://download.csdn.net/download/liangbch/4070516.

 

Algorithm:

The main algorithm of this program is the Newton Iteration Method. First, he obtained an initial value of the square root, and then gradually improved using the Newton Iteration Method. During the calculation process, he always expressed the value of the square root of an integer by comparing two integers. Finally, it converts the square root of the score to a decimal string.

 

Note: This article is to be improved.

 

Performance analysis:

Compared with pifast, its performance is still not very good. I analyzed the time overhead of each part of the program and found that the main bottleneck lies in the final output part. The following table shows the time used to calculate the square root of 2.

Steps

Time (seconds)

Percentage

Calc

0.308532

10.92%

X = POW (10, d)

0.094357

3.34%

P * = (10 ^ d)

0.371915

13.17%

P/Q

0.708144

25.07%

Tostring

1.177165

41.68%

Print to file

0.164422

5.82%

Total

2.824535

100.00%

 

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.