Your question is Optimized Prime Number Code. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Principal Financial Group's Principal Retirement Plan services may need to validate integer identifiers or test values before further processing. Implement an optimized function that determines whether a given integer is prime.
A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Return True for prime inputs and False otherwise.
Implement is_prime(n), where n is an integer. The function must return a Boolean value:
True if n is primeFalse if n is less than 2 or has a divisor other than 1 and itselfYour solution should avoid checking every integer up to n. Explain why checking possible divisors only through sqrt(n) is sufficient, and optimize the loop by handling even numbers separately.
def is_prime(n):