Prime check function

EasyFunction Programs · 10 of 39

Write a Python function that returns True if a number is prime.

Advertisement
Input
7, 12, 1
Output
[(7, True), (12, False), (1, False)]

Approach

  1. Test divisors up to the square root.

Solution

Python
def is_prime(n):    if n < 2:        return False    for d in range(2, int(n ** 0.5) + 1):        if n % d == 0:            return False    return True print([(n, is_prime(n)) for n in [7, 12, 1]])
Output
[(7, True), (12, False), (1, False)]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs