Any Better logic for this problem..

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Jun 9 18:39:53 EDT 2011


Chris Angelico wrote:

> Rather than find all prime numbers up to num, stop at sqrt(num) - it's
> not possible to have any prime factors larger than that.

That's not quite true -- the prime factors of 26 are 2 and 13,
and 13 is clearly greater than sqrt(26).

However, once you've divided out all the primes up to sqrt(n),
whatever is left, if greater than 1, must itself be prime, so
you can add it to your prime factors and stop.

-- 
Greg



More information about the Python-list mailing list