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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.