in reply to Re^5: OT: Finding Factor Closest To Square Root
in thread OT: Finding Factor Closest To Square Root
The original question started "given N and N's prime factorization", so it seems inappropriate to take into account the time taken to factorize.
In this case though, note that 988041964007 = 7 x 11087 x 12731023 - you are losing accuracy by throwing out the BigInts.
In general if the factorization of N is taking longer than it takes to do sqrt(N) trial divisions then the factorization algorithm is pretty bad, since "trial division up to the square root" is the classical example of a slow algorithm.
Many of the high precision maths libraries out there do factorization - for example the pari library (Math::Pari) has a factorint() function:
use Math::Pari qw/ factorint /; my $n = Math::Pari->new("988041964007"); my $f = factorint($n); my $count = $#{ $f->[0] }; print join(" * ", map { my($prime, $power) = ($f->[0][$_], $f->[1][$_]); $power > 1 ? "$prime ^ $power" : $prime } 0 .. $count), "\n"; 7 * 11087 * 12731023
Note that this algorithm does not guarantee primality: for large factors you need to verify with an isprime() check.
Hugo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: OT: Finding Factor Closest To Square Root
by BrowserUk (Patriarch) on Feb 21, 2005 at 17:56 UTC | |
by tall_man (Parson) on Feb 22, 2005 at 17:36 UTC | |
by BrowserUk (Patriarch) on Feb 22, 2005 at 19:14 UTC | |
by tall_man (Parson) on Feb 22, 2005 at 20:44 UTC |