Skip to content

Instantly share code, notes, and snippets.

@abdollar
Forked from peterc/sroot.rb
Created December 21, 2011 06:10
Show Gist options
  • Save abdollar/1504845 to your computer and use it in GitHub Desktop.
Save abdollar/1504845 to your computer and use it in GitHub Desktop.
Square root calculation using Newton-Raphson
# Square root calculation using Newton-Raphson
# from Practical Programming (1968)
a = 256
x = (1 + a) / 2.0
loop do
ox = x
x = (x + a.to_f / x) / 2.0
break if x >= ox
end
puts "The square root of #{a} is #{x}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment