Created
March 16, 2017 09:21
-
-
Save thinkphp/028a173ebf89e31059e9146cd4f15b15 to your computer and use it in GitHub Desktop.
SQuare RooT in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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