annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
238
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
802
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
3 """
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
4 print prime numbers for each argument given
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
5 """
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
6
238
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 def prime(number):
802
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
8 """determines if `number` is prime"""
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
9 # XXX this is owefully inefficient and is written as
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
10 # a (bad) example only
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
11
238
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 half = int(number / 2)
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 for i in range(2, half):
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 if not number % i:
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 return False
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 return True
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
253
8593b0384d3e add a function for computing primes
Jeff Hammel <jhammel@mozilla.com>
parents: 238
diff changeset
18 def primes(n):
8593b0384d3e add a function for computing primes
Jeff Hammel <jhammel@mozilla.com>
parents: 238
diff changeset
19 return [i for i in range(2,n)
8593b0384d3e add a function for computing primes
Jeff Hammel <jhammel@mozilla.com>
parents: 238
diff changeset
20 if not [True for j in range(2,1 + i/2)
8593b0384d3e add a function for computing primes
Jeff Hammel <jhammel@mozilla.com>
parents: 238
diff changeset
21 if not i%j]]
8593b0384d3e add a function for computing primes
Jeff Hammel <jhammel@mozilla.com>
parents: 238
diff changeset
22
8593b0384d3e add a function for computing primes
Jeff Hammel <jhammel@mozilla.com>
parents: 238
diff changeset
23
238
05728a85e19b add prime.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if __name__ == '__main__':
802
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
25 import argparse
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
26 parser = argparse.ArgumentParser(description=__doc__)
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
27 parser.add_argument('arg', type=int, nargs='+',
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
28 help="(positive) integer to find the primes for")
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
29 options = parser.parse_args()
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
30 for arg in options.arg:
b5a59c3e4421 more fixups, for example, but lets throw it away anyway
Jeff Hammel <k0scist@gmail.com>
parents: 253
diff changeset
31 print prime(arg)