The overloaded operators are shunned for lowlevel routines, which are roughly 10x faster. We also add some code to quickly return the factors of perfect squares, since we already compute sqrt($n) for our bound.
This is a smarter trial division algorithm than usually implemented, it checks at each step whether it should continue instead of blindly checking every number between 2 and sqrt(n). It can still be optimized more by using lower-level Rmpz_* functions instead of the operator overloading of % and /. This is the fastest factoring algorithm to use if the input number has a factor less than roughly 1000.