Menu Close

How to get a list all primes below N with Python?

Sometimes, we want to get a list all primes below N with Python.

In this article, we’ll look at how to get a list all primes below N with Python.

How to get a list all primes below N with Python?

To get a list all primes below N with Python, we can use the sympy library.

For instance, we write:

from sympy import sieve
primes = list(sieve.primerange(1, 10**2))
print(primes)

We import the sieve module from sympy.

Then we call the sieve.primerange method with the min and max of the range of which want to get the prime numbers for.

So we get all the primes between 1 and 100.

Therefore, primes is [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].

Conclusion

To get a list all primes below N with Python, we can use the sympy library.

Posted in Python, Python Answers