How to get prime numbers quickly in python using library functions ?

Library functions always makes our code easy so here we are going to discuss some library function in python to work upon prime numbers. **SymPy** is a python module which contains some really cool prime number related library functions. Given below is the list of these functions :

**isprime(n) :**It tests if n is a prime number (True) or not (False).**primerange(a, b) :**It generates a list of all prime numbers in the range [a, b).**randprime(a, b) :**It returns a random prime number in the range [a, b).**primepi(n) :**It returns the number of prime numbers less than or equal to n.**prime(nth) :**It returns 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) :**It returns the prev prime smaller than n.**nextprime(n) :**It returns the next greater prime than n.**sieve.primerange(a, b) :**It generates all prime numbers in the range [a, b), implemented as a dynamically growing sieve of Eratosthenes.

`# Library functions for prime ` `import` `sympy ` ` ` `# Output : True ` `print` `sympy.isprime(` `5` `) ` ` ` `# Output : [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] ` `print` `list` `(sympy.primerange(` `0` `, ` `100` `)) ` ` ` `print` `sympy.randprime(` `0` `, ` `100` `) ` `# Output : 83 ` `print` `sympy.randprime(` `0` `, ` `100` `) ` `# Output : 41 ` `print` `sympy.prime(` `3` `) ` `# Output : 5 ` `print` `sympy.prevprime(` `50` `) ` `# Output : 47 ` `print` `sympy.nextprime(` `50` `) ` `# Output : 53 ` ` ` `# Output : [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] ` `print` `list` `(sympy.sieve.primerange(` `0` `, ` `100` `)) ` |

*chevron_right*

*filter_none*

**Reference :**

https://stackoverflow.com/questions/13326673/is-there-a-python-library-to-list-primes

This article is contributed by **Shashank Mishra (Gullu)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Python | sympy.prime() method
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Prime numbers after prime P with sum S
- Sum of each element raised to (prime-1) % prime
- Print the nearest prime number formed by adding prime numbers to N
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Print prime numbers with prime sum of digits in an array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of prime numbers without odd prime digits
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array