Mercurial > hg > config
annotate python/example/factorial.py @ 924:fc88a12acf48 default tip
add lfs filter
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 04 Dec 2024 15:50:53 -0800 |
parents | 47a434dd5068 |
children |
rev | line source |
---|---|
784
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 """how many 0s are in a factorial""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 import argparse |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 import sys |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 def factorial(N): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 """factorial of `N`""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 if N == 1: |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 return 1 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 return reduce(int.__mul__, range(2, N+1)) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 def factorial_zeros(N): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 """how many 0s are in a factorial?""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 return N/5 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 def main(args=sys.argv[1:]): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 """CLI""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 parser = argparse.ArgumentParser(description=__doc__) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 parser.add_argument('N', type=int, nargs='+') |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 options = parser.parse_args(args) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 # sanity |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 if any([i < 1 for i in options.N]): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 parser.error("Input values must be >= 1") |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 for i in options.N: |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 f = factorial(i) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 print f |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 if __name__ == '__main__': |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 main() |