...

/

Solution Review: Check Whether a Number is Prime

Solution Review: Check Whether a Number is Prime

This review explains the solution for the "checking prime" problem.

We'll cover the following...

Solution

Press + to interact
def is_prime(n):
"""Tests if a number n is prime."""
if n <= 1: # Adding this condition
return False
divisor = 2
while divisor <= n // divisor:
if n % divisor == 0:
return False
divisor += 1
return True
n = 1
print(is_prime(n))

Explanation

Over here, we have to add in the condition for when n is less than or equal to 1, as you can see in line 3 of the above code. If n is either less than or equal ...