list - Improving pure Python prime sieve by recurrence formula -


I am trying to further optimize the champion solution in the main number thread by removing the complex formula for the length of the sub-list . LAN () is very slow because the lane is very expensive and generating later versions is expensive. It looks to make the fashion a little faster, but I can not remove the partition yet, although I only divide it into the condition statement. Of course I can try to simplify the calculation of the length by removing the optimization of the starting point for n instead of n * n ...

I have been dividing the integer / integer part 3 with integer division Apart from this, I would be interesting that this recurring formula can help speed up the blue solution, or divide it by

  __future__ from the import partition  

But I do not experience it much using numpy.

If you enable the psyco for code, the story is completely different, though the Atkins sieve code gets faster than this special piece of technology.

  import cProfile def rwh_primes1 (n): # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python / 3035188 # 3035188 "" "gives a list of prime & lt; n" "sieve = [right] * (n2) 2 for xrange (3, int (n ** 0.5) +1.2): If sieve [i / 2]: sieve [i * i // 2: i] = [wrong] * ((ni * i-1) // (2 * i) + 1) returns [2] + [2 * For i + 1, I am in xrange (1, n / 2) if sieve [i]] DEF Prime (n): # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all- Primes-below-n-in-dragon / 3035188 # 3035188 # Repetition formula for length # 1 and zodiac 2 Tony Vazaline 2010 "" "Take a list of primers Tata is & lt; N "" "run = [right] * (n2 2) zodiac 1 = n-10 for zodiac 2 = 6 i am for example (3, int (n ** 0.5) +1.2): if sieve [i 2 ]: ## Are you the complete reciprocal sieve [i * i // 2 :: i] = [wrong] * (amount 1 / zodiac 2 + 1) amount 1 - = 4 * i + 4 zodiac 2 + = 4 For recursive formulas, you can create reverse [2] + [2 * i + 1 for xchange (1, n 2/2) if sieve [i]] number = 1000000 print ('profiling') cProfile.Profile.bias = 4e-6 for testing (Rwh_primes1, primes): cProfile.run ("test (numprimes)")  

Profiling Between so much difference)

  3 function call in 0.191 CPU seconds: standard name ncalls tottime percall cumtime percall filename: lineno (function) 1 0.006 0.006 0.191 0.191 & lt; string & gt; : 1 (Module & gt;) 1 0.185 0.185 0.185 0.185 myprimes.py:3 (rwh_primes1) 1 0.000 0.000 0.000 0.000 {Method of 'idle' '_lsprof.Profiler' objects} 0.1 9 2 CPUs in 3 functions Order by call: Standard Name EncolvolumnTotal Percall cumtime Percall File Name: Linux (function) 1 0.006 0.006 0.192 0.192 & lt; String & gt ;: 1 (Module & gt;) 1 0.186 0.186 0.186 0.186 myprimes.py:12 (Primes) 1 0.000 0.000 0.000 0.000 {Method '' _lsprof.Profiler 'objects of' idle '}  

Interestingly, extend the boundary to 10 ** 8 and set the time decorator to remove the tasks:

  rwh_primes1 took 23.670s primes took 22.792 s primesieve 10.850 S  

Interesting if you do not produce a list of primes, but the sieve is about halfway back from the list list itself.

You can adapt a wheel, the coefficients of 2 and 3 are not prime, so they are at all Do not insert You can then start at 5 and increase the phases of 2,4,2,4,2,4 etc. and leave multiples of 2 and 3.

Below it is a C ++ code for this hope that it helps.

  zero sieve23 () {int lim = sqrt (max); For (int i = 5, bit1 = 0; i  

Comments