Optimizing a prime number generator in Python -
i looking suggestions on optimizing prime number generator. please include correction , little comment on why faster in response.
def primelist ( highestnumber ): """ function takes integer , returns list of primes less or equal integer""" numbers = range( 2, highestnumber + 1 ) # creates inclusive list of numbers between 2 , highestnumber isprime = [ true ] * len( numbers ) # each element corresponds element in numbers , keeps track of whether or not prime primes = [] # i'll build list of prime numbers in range( len( numbers ) ): if ( isprime[i] == true ): increment = numbers[i] position = + increment primes.append( numbers[ ] ) while ( position < len( numbers )): # execute if above if statement true because position still greater len( number ) isprime[position] = false # sets element of isprime false if multiple of lower number position += increment return primes
there's great discussion on various prime number generators here: fastest way list primes below n
at link python script can use compare algorithm against several others.
Comments
Post a Comment