Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Calculate prime factors for a given numer in a perl one-liner

by Anonymous Monk
on Nov 17, 2011 at 08:25 UTC ( #938539=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Calculate prime factors for a given numer in a perl one-liner
in thread Calculate prime factors for a given numer in a perl one-liner

Just for info, I did the same course. Didn't googled for a solution. I've come with that solution. Now I'm googling it just to see how other perl user are solving that easy problem :-) and what I see is quite interesting (especially this things with regex!!) It might not be the speediest, but still it might be of some interest to some people!

perl -le '$_=2;while($ARGV[0]-1){if(!($ARGV[0]%$_)){print$_ ;$ARGV[0]/ +=$_;}else{$_++}}' $1
or in a non-one-liner form:
$_=2; while ($ARGV[0]-1) { if (!($ARGV[0]%$_)) { print $_ ; $ARGV[0]/=$_; } else { $_++ } }
Sincerely yours, Alessandro


Comment on Re^3: Calculate prime factors for a given numer in a perl one-liner
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (20)
As of 2014-07-24 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (162 votes), past polls