1

Two simple generators for generating prime numbers using a prime sieve. gen_sieve(n) will produce all prime numbers isprime is an example of how one could use gen_sieve: a quick function for testing primality by attempting modulo division with each potential prime factor of a number.
This is the fastest prime sieve I could come up with in python. There might be a way to reduce the memory footprint.
Tags: algorithms

2 comments
Add a comment
Sign in to comment
2 is prime. 2 is a prime number... Your algorithm says it's not.
I ran it using the following code:
def main(): testnos = (1,2,3,4,5,6,7,8,9,10,11,12,13,41) for testno in testnos: factor = isprime(testno) if factor: print ("Number is not prime: %i  factor is: %i\n" % (testno,factor)) else: print ("Prime Number!  %i\n" % testno) if __name__ == '__main__': main()
Output: Prime Number!  1
Number is not prime: 2  factor is: 2
Prime Number!  3
Number is not prime: 4  factor is: 2
Prime Number!  5 ...
Oh, whoops! I didn't catch that before. It should be fixed now.