Skip to content

Instantly share code, notes, and snippets.

@pgebert
Last active January 14, 2022 08:34
Show Gist options
  • Save pgebert/36d312fd2f7d2bdf279e8a21dfa6ba66 to your computer and use it in GitHub Desktop.
Save pgebert/36d312fd2f7d2bdf279e8a21dfa6ba66 to your computer and use it in GitHub Desktop.
Primality test for integers in python.
/**
* Created by pgebert on 14/01/22.
*
* Test whether a number is prime or not in python.
*/
def is_prime(n: int) -> bool:
"""Primality test for integers."""
if n <= 3:
return n > 1
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i ** 2 <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment