# XXX Incomplete! argv parser not done! #!/usr/bin/env python from math import sqrt from time import time, clock from sys import argv, exit if '-m' in argv: max = argv.pop( if '--help' in [arg in argv]: exit(''' diskprimes.py [-m int] [-o path] [-v int] prints prime numbers to a file and a nice summary to stdout. -m: highest number to try for primitude || 65535 -o: filename to print to (will be overwritten) || 'primes-' + ugly hex. -v: gloat verbosely every this many primes; 0 to disable summary too. ''') for i in argv: try: out = argv[argv.index('-o')+1] except: outfile = 'primes_$i' % time() try: max = int(argv[argv.index('-m')+1]) except: max = 0xffff try: update = int(argv[argv.index('-v')+1]) except: update = None give = open(out, 'w') take = open(out, 'r') if update: print 'total:\tlast:\ttime:\ttotal/sec:' start = time() # time th' math, not the fopen()s and getopts c = 3L # candidate p = [c] # primes # dumb special case: print >> give, '1\n2\n3' give.flush() take.seek(2) count = 3 while c <= max: if sqrt(c) >= p[-1]: give.flush() p.append(int(take.readline())) for i in p: if c % i == 0: break if c % i != 0: # c's prime print >> give, c count += 1 if update: if count % update == 0: print '%d\t%d\t%.3f\t%.3f' \ % (count, c, (time()-start), count/(time()-start)) c += 2 print 'Wrote the %s primes <= %d in %.3f seconds (%.3f/s)' \ % (count, max, (time()-start), count/(time()-start))