package rebelsky.sqrt; import java.math.BigDecimal; public class MyMath1 { public static BigDecimal sqrt(BigDecimal n, BigDecimal a) throws Exception { BigDecimal d2= BigDecimal.valueOf(2.0); if ( n.divide(a)==a) { return a; } // if (the solution is close enough) else { return sqrt(n, (a.add((n.divide(a, BigDecimal.ROUND_HALF_EVEN )))).divide(d2)) ; } // if the solution is not close enough } } /* * Potential problems * * Should probably stop when "close enough" and not when equal * * Didn't quite get the letter of the instructions right - Spirit * was "compute square roots"; letter was "n, epsilon as params, * call helper that also takes a" * * Nesting might be confusing, but might be necessary. * * Boy, that line is long. * * Formatting: Braces are not used in the "standard" way. * * Did not use recommended form of division. */