Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: porting C code to Perl -- solved

by soonix (Canon)
on Oct 23, 2017 at 18:41 UTC ( [id://1201919]=note: print w/replies, xml ) Need Help??


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
  • $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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201919]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-04-18 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found