annotate Demo/scripts/fact.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 6587ea6d3e78
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
1 #! /usr/bin/env python
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
504
eb03977baeeb Adapt to mixed-mode arithmetic, and added a warning comment.
Guido van Rossum <guido@python.org>
parents: 274
diff changeset
3 # Factorize numbers.
eb03977baeeb Adapt to mixed-mode arithmetic, and added a warning comment.
Guido van Rossum <guido@python.org>
parents: 274
diff changeset
4 # The algorithm is not efficient, but easy to understand.
eb03977baeeb Adapt to mixed-mode arithmetic, and added a warning comment.
Guido van Rossum <guido@python.org>
parents: 274
diff changeset
5 # If there are large factors, it will take forever to find them,
eb03977baeeb Adapt to mixed-mode arithmetic, and added a warning comment.
Guido van Rossum <guido@python.org>
parents: 274
diff changeset
6 # because we try all odd numbers between 3 and sqrt(n)...
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import sys
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 from math import sqrt
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def fact(n):
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
12 if n < 1:
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
13 raise ValueError('fact() argument should be >= 1')
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
14 if n == 1:
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
15 return [] # special case
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
16 res = []
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
17 # Treat even factors special, so we can use i += 2 later
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
18 while n % 2 == 0:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
19 res.append(2)
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
20 n //= 2
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
21 # Try odd numbers up to sqrt(n)
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
22 limit = sqrt(n+1)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
23 i = 3
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
24 while i <= limit:
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
25 if n % i == 0:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
26 res.append(i)
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
27 n //= i
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
28 limit = sqrt(n+1)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
29 else:
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
30 i += 2
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
31 if n != 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
32 res.append(n)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
33 return res
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def main():
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
36 if len(sys.argv) > 1:
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
37 source = sys.argv[1:]
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
38 else:
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
39 source = iter(raw_input, '')
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
40 for arg in source:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
41 try:
56457
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
42 n = int(arg)
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
43 except ValueError:
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
44 print arg, 'is not an integer'
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
45 else:
6587ea6d3e78 Modernize factorisation demo (mostly augassign.)
Georg Brandl <georg@python.org>
parents: 49168
diff changeset
46 print n, fact(n)
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
33456
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
48 if __name__ == "__main__":
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
49 main()