Efficient Ways Of Finding The Largest Prime Factor Of A Number
Solution 1:
From your approach you are first generating all divisors of a number n
in O(n)
then you test which of these divisors is prime in another O(n)
number of calls of test_prime
(which is exponential anyway).
A better approach is to observe that once you found out a divisor of a number you can repeatedly divide by it to get rid of all of it's factors. Thus, to get the prime factors of, say 830297
you test all small primes (cached) and for each one which divides your number you keep dividing:
830297
is divisible by13
so now you'll test with830297 / 13 = 63869
63869
is still divisible by13
, you are at4913
4913
doesn't divide by 13, next prime is17
which divides4913
to get289
289
is still a multiple of17
, you have17
which is the divisor and stop.
For further speed increase, after testing the cached prime numbers below say 100
, you'll have to test for prime divisors using your test_prime
function (updated according to @Ben's answer) but go on reverse, starting from sqrt
. Your number is divisible by 71
, the next number will give an sqrt
of 91992
which is somewhat close to 6857
which is the largest prime factor.
Solution 2:
Here is my favorite simple factoring program for Python:
def factors(n):
wheel = [1,2,2,4,2,4,2,4,6,2,6]
w, f, fs = 0, 2, []
while f*f <= n:
while n % f == 0:
fs.append(f)
n /= f
f, w = f + wheel[w], w+1if w == 11: w = 3if n > 1: fs.append(n)
return fs
The basic algorithm is trial division, using a prime wheel to generate the trial factors. It's not quite as fast as trial division by primes, but there's no need to calculate or store the prime numbers, so it's very convenient.
If you're interested in programming with prime numbers, you might enjoy this essay at my blog.
Solution 3:
My solution is in C#. I bet you can translate it into python. I've been test it with random long integer ranging from 1 to 1.000.000.000 and it's doing good. You can try to test the result with online prime calculator Happy coding :)
publicstaticlongbiggestPrimeFactor(long num){
for (int div = 2; div < num; div++) {
if (num % div == 0) {
num \= div
div--;
}
}
return num;
}
Solution 4:
The naive primality test can be improved upon in several ways:
- Test for divisibility by 2 separately, then start your loop at 3 and go by 2's
- End your loop at ceil(sqrt(num)). You're guaranteed to not find a prime factor above this number
- Generate primes using a sieve beforehand, and only move onto the naive way if you've exhausted the numbers in your sieve.
Beyond these easy fixes, you're going to have to look up more efficient factorization algorithms.
Solution 5:
Use a Sieve of Eratosthenes to calculate your primes.
from math import sqrt
defsieveOfEratosthenes(n):
primes = range(3, n + 1, 2) # primes above 2 must be odd so start at three and increase by 2for base in xrange(len(primes)):
if primes[base] isNone:
continueif primes[base] >= sqrt(n): # stop at sqrt of nbreakfor i in xrange(base + (base + 1) * primes[base], len(primes), primes[base]):
primes[i] = None
primes.insert(0,2)
returnfilter(None, primes)
Post a Comment for "Efficient Ways Of Finding The Largest Prime Factor Of A Number"