At the HPE Training Center, I met the problem of asking for cube root, and here we do some algorithmic notes,
Analysis Process:
The cubic root of the number n is n=i*i**i, so we will think of the method first.
Static double g32 (double// simple double i = 0, k = 0.0005f; if (N < 0) { // Input Negative number to judge K/=-1; } Do { I+ =k; } while // ABS for their own write the absolute value method return i; }
It can be seen that the solution precision is 0.001, and when the input data is too large, the efficiency is worrying, so there is the following optimization
Static DoubleG33 (DoubleN) {//Optimization 2 Doublei = 0, k =5f; if(N < 0) {k/=-1; } for(intt = 0; T < 15; t++) {//precision to 15 digits after decimal point Do{i+=K; 5 fast approximation of correct values at start time } while(ABS (i * i * i) <ABS (n)); Exit I when I^3>=n-=k;k/= 9.2; I revert to the value before exiting, K shrinks, goes to next approximation}returni; }
This method can quickly obtain the cubic root, the input value n is not too large when used, when n is too large in the approximation process i^3 and (i+k) ^3 gap is too large, the number of cycles increased, into the dead loop state. I'm going to go into a dead loop when my computer is n=9999999999 (10 9).
So think of a solution, set a loop number counter W, so that the K value increases with the number of cycles, to a certain extent, to solve the dead loop problem. Here's the code.
Static DoubleG34 (DoubleN) {//Final Optimization Doublei = 0, k =5f; if(N < 0) {k/=-1; } intW=0; for(intt = 0; T < 15; t++) { Do{i+=K; K=k+w*k/50000; W++; Loop counter} while(ABS (i * i * i) <ABS (n)); I-=k;k/= 9.5; } System.out.println (W); returni; }
A change of n equals 20 9 will not be a dead loop.
Finally, attach all the program code.
Packagecom.gfuzan.test;ImportJava.util.Scanner; Public classTest {/*** Development: Gfuzan * Time: 2017.08.08 * function: Cubic root*/ Public Static voidMain (string[] args) {System.out.print ("Please enter a number:"); Scanner SC=NewScanner (system.in); Doublen =sc.nextdouble (); Sc.close (); System.out.println ("Math: \ T" + Math.pow (n, 1.0/3)); System.out.println ("My simple version: \ t" +g32 (n)); System.out.println ("My optimization 2: \ t" +G33 (n)); System.out.println ("My final optimization: \ t" +g34 (n)); } Static DoubleG32 (DoubleN) {//Simple Version Doublei = 0, k = 0.0005f; if(N < 0) {k/=-1; } Do{i+=K; } while(ABS (I*i*i) <ABS (n)); returni; } Static DoubleG34 (DoubleN) {//Final Optimization Doublei = 0, k =5f; if(N < 0) {k/=-1; } intW=0; for(intt = 0; T < 15; t++) { Do{i+=K; K=k+w*k/50000; W++; } while(ABS (i * i * i) <ABS (n)); I-=k;k/= 9.5; } returni; } Static DoubleG33 (DoubleN) {//Optimization 2 Doublei = 0, k =5f; if(N < 0) {k/=-1; } for(intt = 0; T < 15; t++) { Do{i+=K; } while(ABS (i * i * i) <ABS (n)); I-=k;k/= 9.2; } returni; } Static DoubleAbsDoublef) {if(F < 0) { return0-F; } returnF; }}
Run results
Seeking cubic root algorithm--the individual's exhaustive and optimized for cubic root algorithm