foreach my $i( reverse 1..$len){
my $x = 10 * $a[$i-1] + $q * $i;
$a[$i-1] = $x % (2 * $i - 1);
$q = int($x / (2 * $i - 1));
}
not sure if it's better or worse, but I'd change that to
foreach my $i( reverse 0 .. $len-1){
my $x = 10 * $a[$i] + $q * ($i+1);
my $divisor = 2 * $i + 1;
$a[$i] = $x % $divisor;
$q = int($x / $divisor);
}
not for efficiency, but for (perhaps) easier understanding
- $i one less, so the array index is a simple variable
- the common expression of the last two lines as a variable of its own