An algorithm problem for solving square root

Source: Internet
Author: User
Tags square root

1. Description of the problem

The problem is this, given a number a, to solve the square root of the number, requiring precision f<0.0001.

Long time no fencing implementation algorithm code, today to write a small, follow-up algorithm is still the focus of research. In comparison software, the algorithm is the core of the soul Ah!

2. Algorithm analysis

Say, this algorithm is actually not too troublesome, the main thing to do is to constantly test, approximation is really the goal value of the idea, the most easy to think of is, constant binary approximation, a bit similar to the idea of two points. At the same time, an important thought:

1. Set the target value of square root to SE

2. The input data to be solved is St

3. | Se*se-st| < f*f

4. Find a se*se-st > 0 in the case of the Se value, because the square root points plus or minus two values, to find a can, here to find positive

3. Implementation process

Here, directly on the Java Implementation Code, note that here only the square root is more than 1 of the problem scenario, as for the square root in the 0~1 between this scene, not processed!

Package Pingfanggeng;import java.io.file;import java.io.filenotfoundexception;import java.util.Scanner;/** * @author Shihuc * @date June 19, 2018 8:31:51 * * This algorithm approximately calculates the square root of a number greater than 1*/ Public classSolution {/** * @author Shihuc * @param args*/     Public Static voidMain (string[] args) {Scanner scan=NULL; Try{Scan=NewScanner (NewFile ("./src/pingfanggeng/input.txt")); intCount =Scan.nextint ();  for(inti =0; I < count; i++){                intSource =Scan.nextint (); DoubleFraction =scan.nextdouble (); Doubleres = Bindivfind (0, source, source, fraction*fraction); System. out. println ("No."+ i +": source="+ Source +", fraction="+ fraction +", result="+res); }        } Catch(FileNotFoundException e) {e.printstacktrace (); } finally {            if(Scan! =NULL) {scan.close (); }        }    }        /** * Algorithm ideas: * | Se*se-st| < Fraction*fraction * Where SE is the target value, ST is given the data to be solved, fraction for the given accuracy * * @author SHIHUC * @param src * @para M fraction * @return*/    Private Static DoubleBindivfind (DoubleStDoubleEdDoubleTarDoublefraction) {        DoubleSe = (Double) ((St + ed)/2); DoubleSe2 = Se *Se; DoubleSet = Se2-tar; Doubleres = Set *Set; DoubleRval =Se; if(Res >=fraction) {            if(Set >0) {Rval=Bindivfind (St, Se, tar, fraction); }Else{rval=Bindivfind (Se, ed, tar, fraction); }        }        returnRval; }}

For the test case in the code, see the following file:

6 9876543 0.0001  - 0.001 999 0.0001 777 0.00001 8888 0.001 1000000 0.0001

The results of the test run are as follows:

No.0: source=9876543, fraction=1.0E-4, result=3142.696771881704No.1: source= -, fraction=0.001, result=5.744636535644531No.2: source=999, fraction=1.0E-4, result=31.606961332261562No.3: source=777, fraction=1.0E-5, result=27.874719826038927No.4: source=8888, fraction=0.001, result=94.27618705481291No.5: source=1000000, fraction=1.0E-4, result=999.9999999763531

Analysis and Summary:

1. The realization of the whole algorithm, mainly through the idea of recursion, constantly binary temptation, gradually approaching the target value.

2. To solve the data, in the implementation of the algorithm, if the data type of float, there will be a large error, the use of double words, the accuracy is credible.

3.0~1 between the square root of the scene, just the current algorithm to do a reverse thinking implementation, because the number of 0~1, the number of squares after the number of elements is smaller.

An algorithm problem for solving square root

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.