From 3f6a727acdf2489318a88eefcb5b3b396b5492ce Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Thu, 7 Oct 2021 18:03:16 +0100 Subject: add solution week 133 task 1 in perl --- challenge-133/steven-wilson/perl/ch-1.pl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-133/steven-wilson/perl/ch-1.pl 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; +} -- cgit