Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

primes pytyhon

def is_prime(n):
    if n in (2, 3):
        return True
    if n <= 1 or not (n%6==1 or n%6==5):
        return False
    a, b= 5, 2
    while a <= n**0.5:
        if not n%a:
            return False
        a, b = a+b, 6-b
    return True
# this method is much faster than checking every number because it uses the fact
# that every prime is either 1 above or 1 below a multiple of 6
# and that if a number has no prime factors, it has no factors at all
 
PREVIOUS NEXT
Tagged: #primes #pytyhon
ADD COMMENT
Topic
Name
3+8 =