Possible Duplicate: Fastest way to list all primes below N in python
Although I already have written a function to find all primes under n (primes(10) -> [2, 3, 5, 7]), I am struggling to find a quick way to find the first n primes. What is the fastest way to do this?
Answer
Start with the estimate g(n) = n log n + n log log n*, which estimates the size of the nth prime for n > 5.
Then run a sieve on that estimate.
g(n) gives an overestimate, which is okay because we can simply discard the extra primes generated which are larger than the desired n.
Then consider the answers in "Fastest way to list all primes below N in python".
If you are concerned about the actual runtime of the code (instead of the order of magnitude of the time complexity of the algorithm), consider using one of the solutions that use numpy (instead of one of the "pure python" solutions).
I am trying to find the root y of a function called f using Python. Here is my code:def f(y):w,p1,p2,p3,p4,p5,p6 = y[:7] t1 = w - 0.99006633*(p1**0.5) - (-1.010067)*((1-p1))t2 = w - 22.7235687*(p2**0.…
I have a few functions in my code that are randomly causing SegmentationFault error. Ive identified them by enabling the faulthandler. Im a bit stuck and have no idea how to reliably eliminate this pro…
From what I read/understand, the self parameter is similiar to this.Is that true?If its optional, what would you do if self wasnt passed into the method?
I have this type of image from that I only want to extract the characters.After binarization, I am getting this image img = cv2.imread(the_image.jpg)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh…
I use pyodbc to connect to my local SQL database which works withoout problems.SQLSERVERLOCAL=Driver={SQL Server Native Client 11.0};Server=(localdb)\\v11.0;integrated security = true;DATABASE=eodba;
c…
This question already has answers here:Simple prime number generator in Python(28 answers)Closed 9 years ago.Im trying to write a generator function for printing prime numbers as followsdef getPrimes(n…
I know that this error message (ValueError: too many values to unpack (expected 4)) appears when more variables are set to values than a function returns. scipy.stats.linregress returns 5 values accord…
For example, in Django 1.8:class A(models.Model):x = models.BooleanField(default=True)class B(models.Model):y = models.ManyToManyField(A)class C(models.Model):z = models.ForeignKey(A)In this scenario, …
I have a class that I wish to test via SimpleXMLRPCServer in python. The way I have my unit test set up is that I create a new thread, and start SimpleXMLRPCServer in that. Then I run all the test, and…