Optimized algorithm for determing prime

Optimized algorithm for determing prime.