Skip to content

Instantly share code, notes, and snippets.

@itsmuriuki
Created September 30, 2016 13:02
Show Gist options
  • Save itsmuriuki/33b46e8cfee0318587693f251c76c235 to your computer and use it in GitHub Desktop.
Save itsmuriuki/33b46e8cfee0318587693f251c76c235 to your computer and use it in GitHub Desktop.
square root of numbers
def square_root (n,d) #number and error difference
guess = n / 2.0 #half the guess since square root cant be more than half
while (((guess *guess) - n).abs > d)
guess = refine(guess, n)
end
puts guess
end
def refine (guess, n)
guess = (guess + (n / guess)) / 2.0
end
square_root(9, 0.0000001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment