Cleaner prime detection function