Optimize the isPrime by checking up to sqrt(n), not n/2