Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Python library to list primes?

Tags:

Is there a library function that can enumerate the prime numbers (in sequence) in Python?

I found this question Fastest way to list all primes below N but I'd rather use someone else's reliable library than roll my own. I'd be happy to do import math; for n in math.primes:

like image 799
Colonel Panic Avatar asked Nov 10 '12 22:11

Colonel Panic


People also ask

How do you list primes?

The prime numbers from 1 to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97. Why is 1 not a prime number? 1 is not a prime number because it has only one factor, namely 1. Prime numbers need to have exactly two factors.


2 Answers

SymPy is another choice. It is a Python library for symbolic mathematics. It provides several functions for prime.

isprime(n)              # Test if n is a prime number (True) or not (False).  primerange(a, b)        # Generate a list of all prime numbers in the range [a, b). randprime(a, b)         # Return a random prime number in the range [a, b). primepi(n)              # Return the number of prime numbers less than or equal to n.  prime(nth)              # Return the nth prime, with the primes indexed as prime(1) = 2. The nth prime is approximately n*log(n) and can never be larger than 2**n. prevprime(n, ith=1)     # Return the largest prime smaller than n nextprime(n)            # Return the ith prime greater than n  sieve.primerange(a, b)  # Generate all prime numbers in the range [a, b), implemented as a dynamically growing sieve of Eratosthenes.  

Here are some examples.

>>> import sympy >>>  >>> sympy.isprime(5) True >>> list(sympy.primerange(0, 100)) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] >>> sympy.randprime(0, 100) 83 >>> sympy.randprime(0, 100) 41 >>> sympy.prime(3) 5 >>> sympy.prevprime(50) 47 >>> sympy.nextprime(50) 53 >>> list(sympy.sieve.primerange(0, 100)) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 
like image 77
SparkAndShine Avatar answered Sep 29 '22 10:09

SparkAndShine


The gmpy2 library has a next_prime() function. This simple function will create a generator that will provide an infinite supply of primes:

import gmpy2  def primes():     n = 2     while True:         yield n         n = gmpy2.next_prime(n) 

If you will be searching through primes repeatedly, creating and reusing a table of all primes below a reasonable limit (say 1,000,000) will be faster. Here is another example using gmpy2 and the Sieve of Eratosthenes to create a table of primes. primes2() returns primes from the table first and then uses next_prime().

import gmpy2  def primes2(table=None):      def sieve(limit):         sieve_limit = gmpy2.isqrt(limit) + 1         limit += 1         bitmap = gmpy2.xmpz(3)         bitmap[4 : limit : 2] = -1         for p in bitmap.iter_clear(3, sieve_limit):             bitmap[p*p : limit : p+p] = -1         return bitmap      table_limit=1000000     if table is None:         table = sieve(table_limit)      for n in table.iter_clear(2, table_limit):         yield n      n = table_limit     while True:         n = gmpy2.next_prime(n)         yield n 

You can adjust table_limit to suit your needs. Larger values will require more memory and increase the startup time for the first invocation of primes() but it will be faster for repeated calls.

Note: I am the maintainer of gmpy2.

like image 44
casevh Avatar answered Sep 29 '22 09:09

casevh