Optimized test_prime