in reply to Re^2: porting C code to Perl -- solved
in thread porting C code to Perl

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

Replies are listed 'Best First'.
Re^4: porting C code to Perl -- solved
by Discipulus (Canon) on Oct 23, 2017 at 19:59 UTC
    Hi soonix,

    your version sounds much perlish! infact I translated the code line by line.

    I totally agree that your version is more readable and .. explicit. It also avoid the divisor repetition.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.