Editor

Prime Number Check

Loops · Intermediate · 80 XP · about 8 min

Write a function called is_prime(n) that returns True if n is a prime number, and False otherwise.

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Example

is_prime(7)    # Returns True
is_prime(4)    # Returns False
is_prime(2)    # Returns True
is_prime(1)    # Returns False
is_prime(17)   # Returns True

Requirements

  • Return False for numbers less than 2
  • Return True for prime numbers
  • 2 is the smallest prime number
def is_prime(n):
# Your code here
pass
# Test your function
print(is_prime(7))
print(is_prime(4))
print(is_prime(2))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.