Optimize the prime finder function