blob: 788bea2b670199e03619b2590a4f71fe46e7e076 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#! /usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use feature 'signatures';
no warnings qw(experimental::signatures);
my $N = $ARGV[0] // die 'Please specify a positive integer';
die "Postive number only" unless $N =~ /^[1-9]\d+$/;
say int_sqare_root($N);
sub int_sqare_root ($number)
{
my $x0 = $number >> 1;
if ($x0)
{
my $x1 = ( $x0 + $number / $x0 ) >> 1;
while ($x1 < $x0)
{
$x0 = $x1;
$x1 = ( $x0 + $number / $x0 ) >> 1;
}
return $x0;
}
else
{
return $number;
}
}
|