$"x=$%="@ARGV";$~=$;='( +)';while($%>>$:++){if($"=~/$~$/^$"=~/$;$/){$ |=$?=$:>2||die"prime\n";eval"print length(\$$?)".($:>++$?&&"/length(\$ $?).'*'")while$:>$?;die$/}$;=$~;$~=~s~.*~^($&\\1+)~;$~=~s;\d+;1+$&;eg}

The best thing I can say about this code is that it works. It will take a number via argv give you the prime factorization of that number... eventually. It's not very efficient. At all. I had a lot of fun writing it, but it's really bad at what it does. To give you an idea of how erratic it is, here are some benchmarks:

Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 10000 2*2*2*2*5*5*5*5 real 0m4.001s user 0m3.977s sys 0m0.015s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 32767 7*31*151 real 0m6.891s user 0m6.832s sys 0m0.031s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 32768 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2 real 2m50.119s user 2m49.588s sys 0m0.015s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 32769 3*3*11*331 real 0m13.282s user 0m13.213s sys 0m0.015s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 65535 3*5*17*257 real 1m11.228s user 1m8.422s sys 0m0.061s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 65536 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2 real 22m20.875s user 20m55.277s sys 0m0.920s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 65537 prime real 0m0.903s user 0m0.842s sys 0m0.031s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 65538 2*3*3*11*331 real 1m40.633s user 1m40.183s sys 0m0.077s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 100000 2*2*2*2*2*5*5*5*5*5 real 20m59.463s user 20m53.998s sys 0m0.202s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 100003 prime real 0m2.090s user 0m2.043s sys 0m0.046s Kevin@muzyx /cygdrive/c/users/kevin $ time perl lol.pl 1000003 prime real 3m23.670s user 3m22.411s sys 0m0.062s

While it can distinguish primes at a reasonable rate, it takes a really long time to factor composite numbers. Particularly composite numbers made up of several small primes. Powers of 2 are the worst-case scenario.

for$b(-25..25){for$a(-50..29){$x=$a/21;$y=$b/15;print$b?chr:chr^chr ord(substr'<6C}'.1x29 .'[FDEq2?@E96Cqa6C=q924',$a)-49;$_=30;($y,$x) =(2*$x*$y+$b/15,$x*$x-$y*$y+$a/21)while$x*$x+$y*$y<9&$_++<95}$_=10}

Replies are listed 'Best First'.
Re: How not to do prime factorization
by atcroft (Abbot) on Jun 30, 2015 at 16:46 UTC

    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:

    Enjoy.

Re: How not to do prime factorization
by QM (Parson) on Jun 30, 2015 at 15:49 UTC
    Geez! A really naive regex solution is very quick, but limited to <= 65535:
    #!/usr/bin/env perl use strict; use warnings; my $candidate = shift; my $candidate_string= ('x' x $candidate); while (1) { if (my ($factor,$rest) = $candidate_string=~ m/^(..+?)(\1+)$/) { print length($factor), "*"; $candidate /= length($factor); $candidate_string= ('x' x $candidate); } else { print $candidate; last; } } print "\n"; exit;

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of