Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Latest commit

 

History

History
26 lines (21 loc) · 609 Bytes

is-prime.md

File metadata and controls

26 lines (21 loc) · 609 Bytes
title type tags cover dateModified
Number is prime
snippet
math
carrots
2020-11-02 19:28:05 +0200

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