diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2021-10-07 18:03:16 +0100 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2021-10-07 18:03:16 +0100 |
| commit | 3f6a727acdf2489318a88eefcb5b3b396b5492ce (patch) | |
| tree | 28e7655df930f667826bdb60e37fb99af7f69a25 | |
| parent | 553831e0c731bc58c0c24beb0e2ca488faacf77d (diff) | |
| download | perlweeklychallenge-club-3f6a727acdf2489318a88eefcb5b3b396b5492ce.tar.gz perlweeklychallenge-club-3f6a727acdf2489318a88eefcb5b3b396b5492ce.tar.bz2 perlweeklychallenge-club-3f6a727acdf2489318a88eefcb5b3b396b5492ce.zip | |
add solution week 133 task 1 in perl
| -rw-r--r-- | challenge-133/steven-wilson/perl/ch-1.pl | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-133/steven-wilson/perl/ch-1.pl b/challenge-133/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..150a2d5fae --- /dev/null +++ b/challenge-133/steven-wilson/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +my @test_input = qw/ 10 27 85 101 /; +my @test_output = qw/ 3 5 9 10 /; + +foreach my $i ( 1 .. ( scalar @test_input ) ) { + ok( find_isqrt( $test_input[ $i - 1 ] ) == $test_output[ $i - 1 ], "test $i" ); +} +done_testing(); + +sub find_isqrt { + my $input = shift; + my $x = $input; + use integer; + my $y = ( $x + 1 ) / 2; + while ( $y < $x ) { + $x = $y; + $y = ( ( $x + $input / $x ) / 2 ); + } + return $x; +} |
