Unit 4 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Check if n is less than 2, then test for factors from 2 up to the square root of n.
1) If n is less than or equal to 1, return False (not a prime).
2) Use a loop to check divisibility from 2 up to the square root of n:
a) If n is divisible by any number in this range, it's not a prime, return False.
b) If no divisors are found, it's a prime, return True.
⚠️ Common Mistakes
n
def is_prime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True