1. Learn
  2. /
  3. Courses
  4. /
  5. Practicing Coding Interview Questions in Python

Exercise

Prime number sequence

A prime number is a natural number that is divisible only by 1 or itself (e.g. 3, 7, 11 etc.). However, 1 is not a prime number.

Your task is, given a list of candidate numbers cands, to filter only prime numbers in a new list primes.

But first, you need to create a function is_prime() that returns True if the input number \(n\) is prime or False, otherwise. To do so, it's sufficient to test if a number is not divisible by any integer number from 2 to \(\sqrt{n}\).

Tip: you might need to use the % operator that calculates a remainder from a division (e.g. 8 % 3 is 2).

The math module is already imported.

Instructions 1/2

undefined XP
    1
    2
  • Define the initial check: numbers lower than 2 are not prime.
  • Define the loop checking if the number n is not prime.