Mercurial > hg > config
comparison python/prime.py @ 802:b5a59c3e4421
more fixups, for example, but lets throw it away anyway
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 28 Oct 2016 16:57:50 -0700 |
parents | 8593b0384d3e |
children |
comparison
equal
deleted
inserted
replaced
801:bea4dd61ae45 | 802:b5a59c3e4421 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 """ | |
4 print prime numbers for each argument given | |
5 """ | |
6 | |
3 def prime(number): | 7 def prime(number): |
8 """determines if `number` is prime""" | |
9 # XXX this is owefully inefficient and is written as | |
10 # a (bad) example only | |
11 | |
4 half = int(number / 2) | 12 half = int(number / 2) |
5 for i in range(2, half): | 13 for i in range(2, half): |
6 if not number % i: | 14 if not number % i: |
7 return False | 15 return False |
8 return True | 16 return True |
12 if not [True for j in range(2,1 + i/2) | 20 if not [True for j in range(2,1 + i/2) |
13 if not i%j]] | 21 if not i%j]] |
14 | 22 |
15 | 23 |
16 if __name__ == '__main__': | 24 if __name__ == '__main__': |
17 import sys | 25 import argparse |
18 for arg in sys.argv[1:]: | 26 parser = argparse.ArgumentParser(description=__doc__) |
19 print prime(int(arg)) | 27 parser.add_argument('arg', type=int, nargs='+', |
28 help="(positive) integer to find the primes for") | |
29 options = parser.parse_args() | |
30 for arg in options.arg: | |
31 print prime(arg) |