Last active
January 14, 2022 08:34
-
-
Save pgebert/36d312fd2f7d2bdf279e8a21dfa6ba66 to your computer and use it in GitHub Desktop.
Primality test for integers in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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