Recognizing that by using a skip number of 2 to select only odd potential primes, it is no longer necessary to test potential primes against all the primes less than the square root of the prime, as none of them can be factored by 2. Therefore, we can remove the first prime number from the set of primes which we test potential primes against. This requires dividing the prime set (ps) array into excepted prime (ep) and test prime (tp) arrays, then recombining them at the end to send the complete set back to the function call.
pp=2 ep=[pp] pp+=1 tp=[pp] ss=[2] lim=raw_input("\nGenerate prime numbers up to what number? : ") while pp<int(lim): pp+=ss[0] test=True sqrtpp=sqrt(pp) for a in tp: if a>sqrtpp: break if pp%a==0: test=False break if test: tp.append(pp) ep.reverse() [tp.insert(0,a) for a in ep] return tp
Read full article from Efficient Prime Number Generating Algorithms - Wikibooks, open books for an open world
No comments:
Post a Comment