modified prime_factor function