diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-10-04 20:58:06 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-10-04 20:58:06 +0100 |
| commit | 823f6b7012dbdd160375aefb6e68ae55a59972c4 (patch) | |
| tree | 33cb6b87c14514de70f3598e56805fe17b4d02e0 | |
| parent | aa3d1f5a4960500481ae284124a31fe241908092 (diff) | |
| download | perlweeklychallenge-club-823f6b7012dbdd160375aefb6e68ae55a59972c4.tar.gz perlweeklychallenge-club-823f6b7012dbdd160375aefb6e68ae55a59972c4.tar.bz2 perlweeklychallenge-club-823f6b7012dbdd160375aefb6e68ae55a59972c4.zip | |
using newtons method - but written compactly
| -rw-r--r-- | challenge-133/james-smith/perl/ch-1.pl | 20 |
1 files changed, 4 insertions, 16 deletions
diff --git a/challenge-133/james-smith/perl/ch-1.pl b/challenge-133/james-smith/perl/ch-1.pl index 5a342656e6..9422daa9e4 100644 --- a/challenge-133/james-smith/perl/ch-1.pl +++ b/challenge-133/james-smith/perl/ch-1.pl @@ -5,8 +5,6 @@ use strict; use warnings; use feature qw(say); use Test::More; -use Benchmark qw(cmpthese timethis); -use Data::Dumper qw(Dumper); my @TESTS = ( [ 10, 3 ], @@ -21,19 +19,9 @@ is( find_root($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); sub find_root { - my $n = shift; - ## Make order of magnitude guess - my ($l,$r) = (1 & length $n ) ? ( 10 ** int(0.5*length $n), 3.2 * 10 ** int(0.5*length $n) ) - : ( 0.3 * 10 ** int(0.5*length $n), 10 ** int(0.5*length $n) ); - while($r - $l > 1) { - print "+"; - my $m = $l + int(( $r-$l)/2); - if( $m*$m < $n ) { - $l=$m; - } else { - $r=$m; - } - } - return $l; + my($x,$y) = (my $n = shift)>>1; + return $n unless $x; + $x = $y while ($y = ($x+$n/$x)>>1) < $x; + return $x; } |
