Files
30-seconds-of-code/snippets/is-prime.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

629 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Number is prime math carrots 2020-10-03T18:03:32+03:00 2020-11-02T19:28:05+02:00

Checks if the provided integer is a prime number.

  • Return False if the number is 0, 1, a negative number or a multiple of 2.
  • Use all() and range() to check numbers from 3 to the square root of the given number.
  • Return True if none divides the given number, False otherwise.
from math import sqrt

def is_prime(n):
  if n <= 1 or (n % 2 == 0 and n > 2):
    return False
  return all(n % i for i in range(3, int(sqrt(n)) + 1, 2))
is_prime(11) # True