Prime number enumeration and primality test in Python

I use it myself occasionally, so I will post it on Qiita (´ ・ ω ・ `)

primes (x) is a method that stores prime numbers less than x in the list. The standard "Eratosthenes sieve" is used as the algorithm. I think the point that does not use filter etc. can be said to be a device.

Next, ʻis_prime (x)is a method that determines whether the integerx` is a prime number. The algorithm is still the standard "trial split". However, we are trying to improve efficiency by using pseudo-prime numbers (numbers that are not divisible by 2, 3, or 5).


import math

#Returns a list of prime numbers greater than or equal to 0 and integers x "less than"
def primes(x):
    if x < 2: return []

    primes = [i for i in range(x)]
    primes[1] = 0 #1 is not a prime number

    #Eratosthenes Sieve
    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0
    
    return [prime for prime in primes if prime != 0]


#Determine if the integer x is a prime number
def is_prime(x):
    if x < 2: return False #There are no prime numbers less than 2
    if x == 2 or x == 3 or x == 5: return True # 2,3,5 is a prime number
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0: return False # 2,3,Multiples of 5 are composite numbers

    #Trial split:Pseudo-prime number(A number that is not divisible by 2, 3, or 5)Divide one after another
    prime = 7
    step = 4
    while prime <= math.sqrt(x):
        if x % prime == 0: return False

        prime += step
        step = 6 - step
    
    return True


if __name__ == '__main__':
    print(primes(30)) #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    ls = [x for x in range(30) if is_prime(x)]
    print(ls) #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Recommended Posts

Prime number enumeration and primality test in Python
Prime number 2 in Python
Algorithm in Python (primality test)
Infinite prime number generator in Python3
Prime number enumeration in one line
Project Euler # 7 "1000 1st prime number" in Python
Primality test by Python
Primality test with Python
Primality test with python
Prime numbers in Python
I made a prime number generation program in Python
I made a prime number generation program in Python 2
[Python 3] Prime factorization in 14 lines
Stack and Queue in Python
Fibonacci and prime implementations (python)
Python debug and test module
Unittest and CI in Python
Set python test in jenkins
Design and test Verilog in Python only with Veriloggen and cocotb.
I tried programming the chi-square test in Python and Java.
Number recognition in images with Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
[python] week1-3: Number type and operation
Prime number generation program by Python
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Write selenium test code in python
Hashing data in R and Python
Function synthesis and application in Python
Statistical test (multiple test) in Python: scikit_posthocs
Export and output files in Python
Study, number guessing game in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
A program that determines whether a number entered in Python is a prime number
[Pytest] [mock] Web development beginners summarized unit test and mock in python.
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Stress Test with Locust written in Python
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Project Euler # 3 "Maximum Prime Factors" in Python
Differences between Ruby and Python in scope
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Write the test in a python docstring
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Prime number