Newton’s Method .to_ruby
here is the math behind Newton’s Method
PRECISION = 0.0001 def newtonian_guess( guess,fx,fdx ) loop do better_guess = guess - ( fx.call( guess ).to_f / fdx.call( guess ).to_f ) return better_guess if ((better_guess - guess).abs <= PRECISION) guess = better_guess end end def sqrt( num ) guess = num/2 return newtonian_guess( guess, lambda {|x| (x**2)-num } , lambda {|x| 2*x } ) end class Float def to_sqrt sqrt( self ) end end p 2.0.to_sqrt # => 1.41421356237469











