Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How not to do prime factorization

by atcroft (Abbot)
on Jun 30, 2015 at 16:46 UTC ( [id://1132640]=note: print w/replies, xml ) Need Help??


in reply to How not to do prime factorization

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found