Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here was my simplistic factoring one-liner, and its results on the first digits from pi (1-11).

Code:

perl -Mstrict -Mwarnings -le 'my $n = shift or die qq{Requires 1 numbe +r argument.}; my $o = $n; my @f = (); my $i = 2; while ( $n % $i == 0 + ) { push @f, $i; $n /= $i; } $i++; while ( $n >= $i ) { while ( $n % + $i == 0 ) { push @f, $i; $n /= $i; } if ( $n > $i ) { $i += 2; } } p +rint $o, q{ = }, join q{*}, @f;'

Output:

$ for number in 3 31 314 3142 31416 314159 3141593 31415927 314159265 +3141592654 31415926536 ; do time -p perl -Mstrict -Mwarnings -le 'my +$n = shift or die qq{Requires 1 number argument.}; my $o = $n; my @f += (); my $i = 2; while ( $n % $i == 0 ) { push @f, $i; $n /= $i; } $i +++; while ( $n >= $i ) { while ( $n % $i == 0 ) { push @f, $i; $n /= +$i; } if ( $n > $i ) { $i += 2; } } print $o, q{ = }, join q{*}, @f;' + $number ; done 3 = 3 real 0.05 user 0.00 sys 0.04 31 = 31 real 0.04 user 0.00 sys 0.03 314 = 2*157 real 0.03 user 0.00 sys 0.03 3142 = 2*1571 real 0.03 user 0.01 sys 0.01 31416 = 2*2*2*3*7*11*17 real 0.03 user 0.00 sys 0.03 314159 = 314159 real 0.07 user 0.04 sys 0.04 3141593 = 7*13*19*23*79 real 0.03 user 0.00 sys 0.01 31415927 = 31*103*9839 real 0.03 user 0.01 sys 0.01 314159265 = 3*3*5*7*127*7853 real 0.03 user 0.03 sys 0.01 3141592654 = 2*139*2131*5303 real 0.03 user 0.00 sys 0.01 31415926536 = 2*2*2*3*3*436332313 real 47.17 user 47.03 sys 0.03

Enjoy.


In reply to Re: How not to do prime factorization by atcroft
in thread How not to do prime factorization by thisisdada

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-04-23 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found