Created
January 17, 2015 22:28
-
-
Save heaven00/af203bd81b0378bc38e4 to your computer and use it in GitHub Desktop.
A prime generator algorithm, it is buggy because beyond starting point 2 nothing is handled and it hangs in range 2 to 1billion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getprimes(start=2, upto=100): | |
""" | |
Generate primes between a given range defined by | |
:start: and :upto: vars. | |
""" | |
#NOTE: np.arrange is faster then python for loop | |
primes = np.arange(2, upto+1) | |
isprime = np.ones(upto-1, dtype=bool) | |
#find the first prime before starting this loop to optimize | |
for factor in primes[:int(upto**0.5)]: | |
if isprime[factor-2]: isprime[factor*2-2::factor]=0 | |
start = start - 2 if start > 2 else start -1 | |
return primes[isprime][start:upto] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://rebrained.com/?p=458