import java.util.function.DoubleFunction;

public class Newton {

    public static double solve(double x0, DoubleFunction<Double> f, DoubleFunction<Double> fprime)
    {
        double tolerance = 1e-7;
        double epsilon = 1e-10;
        double x1 = 0, y, yprime;
        int maxIterations = 50;
        boolean solution = false;

        for (int i = 0; i < maxIterations; i++) {

            y = f.apply(x0);
            yprime = fprime.apply(x0);

            // if denominator is too small
            if (Math.abs(yprime) < epsilon) {
                break;
            }

            x1 = x0 - y / yprime;

            if (Math.abs(x1 - x0) <= tolerance * Math.abs(x1)) {
                solution = true;
                break;
            }

            x0 = x1;
        }

        return solution ? x1 : Double.POSITIVE_INFINITY;
    }
}
