Refactored carmichael? function by extracting a helping function to perform some prechecks.