Computes the cubic root of a number without using library functions
Detailed Description:
Interface description
Prototype:
public static double Getcuberoot (double input)
Input: Double to solve parameter
Return value: Double the cube root of the input parameter
Input Description:
Parameter double type to solve
Output Description:
The cube root of the input parameter is also a double type
Input Example:
216
Output Example:
6.0
importjava.util.Scanner;
Common methodspublic class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(cin.hasNext()) { double input = cin.nextDouble(); double min = 0; double max = input; while(max - min > 0.00001) { double temp = (min + max) / 2; if(temp*temp*temp > input) { max = temp; } else{ min = temp; } } min*=10; double small = min - (int)min; if(small >= 0.5) { min++; } int n = (int)min; min=(double)n/10; System.out.println(min); } }}
Newton Iteration
-
#include <stdio.h>
-
//#include <stdlib.h>
-
-
#define JINGDU 0.0001
-
double Newton_diedai ( double a) {
-
double xn,xn_1;
-
-
Xn=1;
-
xn_1 = xn-((xn*xn*xn-a)/(3*XN*XN));
-
while (Xn-xn_1>jingdu | | xn-xn_1<-jingdu) {
-
xn = xn_1;
-
xn_1 = xn-((xn*xn*xn-a)/(3*XN*XN));
-
}
-
return xn_1;
-
}
-
-
int Main ( void ) {
-
double A;
-
-
scanf ("%lf" , &a);
-
A = Newton_diedai (a);
-
printf ("%.1f" , a);
-
-
System ("pause" );
-
return 0;
-
}
This article is from "Go far." blog, declined to reprint!
Cubic Root Solution (Newton iterative method)