Created
December 6, 2010 08:31
-
-
Save Telltak/730022 to your computer and use it in GitHub Desktop.
A python script to generate and check prime numbers.
Usage for 100 runs(generates 5017 primes):
python prime.py 100
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
from sys import argv | |
from fractions import gcd | |
primes = [2, 3] | |
def isprime(p): | |
if p == 2: return True | |
if p % 2==0: return False | |
max = p**0.5+1 | |
i=3 | |
while i <= max: | |
if p%i==0: return False | |
i+=2 | |
return True | |
for i in range(0, int(argv[1])): | |
q=sorted(primes)[i] | |
for i in range(0, q-2): | |
i+=1 | |
p=int((i**2)+i+q) | |
if isprime(p)==True: | |
if p not in primes: | |
primes.append(p) | |
print len(primes) | |
print sorted(primes)[:200] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you reformat the code.