Skip to content

Instantly share code, notes, and snippets.

@zzak
Created June 30, 2010 16:34
Show Gist options
  • Save zzak/458909 to your computer and use it in GitHub Desktop.
Save zzak/458909 to your computer and use it in GitHub Desktop.
find the square root in ruby
def sqrt(x)
square = lambda { |a| a * a }
average = lambda { |a,b| (a + b)/2.0 }
is_good_enough = lambda { |a| (square[a] - x).abs < 0.001 }
improve = lambda { |a| average[a, x/a] }
sqrt_iter = lambda { |a| is_good_enough[a] ? a : sqrt_iter[improve[a]] }
sqrt_iter[1.0]
end
p sqrt(9)
@AyushJ09
Copy link

AyushJ09 commented Apr 6, 2019

can someone explain this square root program, how is it working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment