Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created May 22, 2012 19:05
Show Gist options
  • Select an option

  • Save wyattdanger/2770984 to your computer and use it in GitHub Desktop.

Select an option

Save wyattdanger/2770984 to your computer and use it in GitHub Desktop.
squareRoot = ( num ) ->
square = ( x ) -> x * x
avg = ( x, y ) -> (x + y) / 2
goodEnough = ( x ) -> Math.abs( square( x ) - num ) < 0.01
improveGuess = ( x ) -> avg( x, num/x )
iter = ( guess ) ->
return guess if goodEnough guess
iter improveGuess guess
iter 1.0
alert squareRoot 101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment