Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 16, 2017 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/028a173ebf89e31059e9146cd4f15b15 to your computer and use it in GitHub Desktop.
Save thinkphp/028a173ebf89e31059e9146cd4f15b15 to your computer and use it in GitHub Desktop.
SQuare RooT in Ruby.
#
# Adrian Statescu <mergesortv@gmail.com>
# This Method Computes SQuare RooT in Ruby.
# MIT License
#
def sqrt(n)
an = n / 2.0
anplus1 = ( an + (n / an).to_f ) / 2
anEPS = 0.0001
while (anplus1 - an).abs >= anEPS
an = anplus1
anplus1 = ( an + (n / an).to_f ) / 2
end
anplus1
end
puts "Enter n = "
n = gets.chomp
puts "sqrt(#{n}) = #{sqrt(n.to_i)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment