fork download
  1. function within(next_result_function, epsilon)
  2. prev_result = next_result_function()
  3. curr_result = next_result_function()
  4. while math.abs(prev_result - curr_result) > epsilon do
  5. prev_result = curr_result
  6. curr_result = next_result_function()
  7. end
  8. return curr_result
  9. end
  10.  
  11. function newton_raphson_sqrt(radicand, epsilon, guess=10.0)
  12. square_root = guess
  13. function next()
  14. square_root = (square_root + radicand / square_root) / 2.0
  15. return square_root
  16. end
  17. return within(next, epsilon)
  18. end
  19.  
  20. print(newton_raphson_sqrt(9.0, 1e-2, 10.0))
  21. print(newton_raphson_sqrt(9.0, 1e-9, 10.0))
  22.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
luac: prog.lua:11: ')' expected near '='
stdout
Standard output is empty