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% |