Use a more generalized trial division in is_prime