Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created August 10, 2013 21:19
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 jimweirich/6202183 to your computer and use it in GitHub Desktop.
Save jimweirich/6202183 to your computer and use it in GitHub Desktop.
Square Root function
# -*- coding: utf-8 -*-
Sqrt = ->(n) {
guess = n / 2.0
loop do
if (guess**2 - n) < 0.0001
return guess
end
guess = (guess + n/guess) / 2.0
end
}
require 'rspec/given'
require 'given/fuzzy_shortcuts'
describe "sqrt" do
Then { Sqrt.(100) == 10.±(0.0001) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment