optimized prime checker