Rewrite algorithm in  isPrime