diff options
| -rw-r--r-- | challenge-133/rage311/perl/ch-1.pl | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-133/rage311/perl/ch-1.pl b/challenge-133/rage311/perl/ch-1.pl new file mode 100644 index 0000000000..5b838433f6 --- /dev/null +++ b/challenge-133/rage311/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use 5.034; +use warnings; +use feature 'signatures'; +no warnings 'experimental::signatures'; +use Test::Simple tests => 4; + +# TASK #1 › Integer Square Root +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# Write a script to calculate the integer square root of the given number. + +sub int_sqrt ($in) { + my $guess = int($in / 2); + while ((my $new_guess = int(($guess + int($in / $guess)) * 0.5)) != $guess) { + $guess = $new_guess; + } + return $guess; +} + +ok(int_sqrt(10) == 3, 'int_sqrt(10) == 3'); +ok(int_sqrt(27) == 5, 'int_sqrt(27) == 5'); +ok(int_sqrt(85) == 9, 'int_sqrt(85) == 9'); +ok(int_sqrt(101) == 10, 'int_sqrt(101) == 10'); |
