// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class CubeRoot {
    public static int cubeRoot1(int v) {
        if(v < 0) {
            return -cubeRoot1(-v);
        } else {
            int res = 0;
            while(res*res*res < v) res++;
            return res;
        }
    }
    public static int cubeRoot2(int v) {
        if(v < 0) {
            return -cubeRoot2(-v);
        } else {
            double xres = 1;
            double tmp;
            do {
                tmp = xres;
                xres = tmp - (tmp*tmp*tmp - v)/(3*tmp*tmp);
            } while(xres - tmp > 0.1 || xres - tmp < -0.1);
            int res = (int)xres;
            if(res*res*res < v) res++;
            return res;
        }
    }
    public static void test(int v) {
        System.out.println("v=" + v);
        System.out.println("cbrt(v) = " + Math.pow(v, 1/3.0));
        System.out.println("algo #1 = " + cubeRoot1(v));
        System.out.println("algo #2 = " + cubeRoot2(v));
    }
    public static void main(String[] args) {
        test(10);
        test(100);
        test(1000);
        test(10000);
        test(-1);
        test(1000000);
    }
}
