|
#!/usr/bin/env perl
our ( $mydir , $myname );
BEGIN {
my $location = (-l $0) ? abs_path($0) : $0;
$location =~ /(.*?)([^\/]+?)_?\z/s or die "?" ;
( $mydir , $myname ) = ($1, $2);
}
sub sumofsquares {
my ( $from , $to ) = @_ ;
my $tot = 0;
for ( my $i = $from ; $i <= $to ; $i ++) {
$tot + = $i * $i ;
}
$tot
}
sub Gsumofsquares {
my ( $from , $to ) = @_ ;
my $tot = 0;
my $i = $from ;
test:
goto calculate if $i <= $to ;
return $tot ;
calculate:
$tot + = $i * $i ;
$i ++;
goto test;
}
TEST {
[ map { [sumofsquares( @$_ ), Gsumofsquares( @$_ )] }
([0, 4], [1, 5], [3, 7], [-2, 4])]
}
[[30, 30], [55, 55], [135, 135], [35, 35]];
repl;
|