Without seeing what you used for primes
, we have to guess (we can't run your code).
But a big part of this is simply mathematics: there are (very roughly speaking) about n/log(n)
primes less than n
, and that's a lot bigger than sqrt(n)
. So when you pass a prime, prime_factor(n)
does a lot more work: it goes through O(n/log(n))
operations before finding the first prime factor (n
itself!), while factors(n)
gives up after O(sqrt(n))
operations.
This can be very significant. For example, sqrt(10000)
is just 100, but there are 1229 primes less than 10000. So prime_factor(n)
can need to do over 10 times as much work to deal with the large primes in your range.