Improved primality checking function to check only odds and up to sqrt of num