$\mathfrak{Mini}$ $\mathbb{Wiki}$

4. April 2021 mozgan   


Is Prime?

This algorithm checks if a given positive integer number is prime or not. If it is, then returns a $\mathtt{True}$, otherwise $\mathtt{False}$.

#!/usr/bin/env python3
import math
def is_prime(n):
    if (n < 2) or (n % 2 == 0):
        return False
    elif n == 2 or n == 3:
        return True
    k = int(math.sqrt(n))
    for i in range(3, k+1, 2):
        if n % i == 0:
            return False
    return True



gives $\mathtt{True}$.


gives $\mathtt{False}$.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies