Number is prime

Python, Math · Nov 2, 2020

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

More like this

  • Prime factors of number

    Finds and returns the list of prime factors of a number.

    Python, Math · May 24, 2023

  • Arithmetic progression

    Generates a list of numbers in the arithmetic progression starting with the given positive integer and up to the specified limit.

    Python, Math · Nov 2, 2020

  • Digitize number

    Converts a number to a list of digits.

    Python, Math · Sep 15, 2020