From 3183f0b695018c7450e8c442b2404595c895efb1 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Tue, 5 Oct 2021 17:41:43 -0300 Subject: Revise Ben Davies' challenge 133 task 1 solution Actually use one of the square root algorithms suggested. --- challenge-133/ben-davies/raku/ch-1.raku | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/challenge-133/ben-davies/raku/ch-1.raku b/challenge-133/ben-davies/raku/ch-1.raku index 98fd70d0ee..44ce0d2ba2 100644 --- a/challenge-133/ben-davies/raku/ch-1.raku +++ b/challenge-133/ben-davies/raku/ch-1.raku @@ -1,4 +1,12 @@ use v6; unit sub MAIN(Int:D $n where * > 0) { - say (1..$n).first({ $^x == $n div $^x }); + # Newton's method. Adapted from Algorithm 1.13 of: + # https://maths-people.anu.edu.au/~brent/pd/mca-cup-0.5.9.pdf + my $x; + my $L = $n; + repeat { + $x = $L; + $L = ($x + $n div $x) div 2; + } until $L >= $x; + say $x; } -- cgit