Refactor the is_prime and prime_factors functions