UPDATE:
Check out @RichieCotton's comment for @agstudy's answer. I agree with Richie, and it actually makes sense to use the @agstudy approach.
Original answer:
Your function is fine, your math is off.
GUESS and x should not (necessarily) be close, but GUESS * GUESS and x should be.
MySqrt <- function (x, eps = 1e-6, itmax = 100, verbose = TRUE){ i <- 1 myvector <- integer(0) GUESS <- readline(prompt="Enter your guess: ") GUESS <- as.integer(GUESS) while(i <= itmax){ GUESS <- (GUESS + (x/GUESS)) * 0.5 myvector <- c(myvector, GUESS) browser(expr={i == 10 || abs(GUESS-x) < eps}) if (abs((GUESS*GUESS)-x) < eps) break
source share