Skip to content

Instantly share code, notes, and snippets.

@colomon
Created January 12, 2014 16: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 colomon/8386731 to your computer and use it in GitHub Desktop.
Save colomon/8386731 to your computer and use it in GitHub Desktop.
sub has-converged($x, $y) {
abs($x-$y) < 1e-8*$y;
}
sub my-sqrt($n) {
my @approximations = 1, -> $x { 0.5 * ($x + $n/$x) } ... &has-converged;
@approximations[*-1];
}
for 1..10 -> $n {
say "$n: { my-sqrt($n) } (type { my-sqrt($n).WHAT.gist })";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment