summaryrefslogtreecommitdiffstats
path: root/Demo/scripts
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-10-10 21:57:03 (GMT)
committerGeorg Brandl <georg@python.org>2009-10-10 21:57:03 (GMT)
commit8bef554cf7716921368ff5422ae437b62cf536a4 (patch)
tree0bb6109c28486d5a39740780929f9473b9e13c72 /Demo/scripts
parent4630c09351ecc460a03950cb93eacd9fa36799f1 (diff)
downloadcpython-8bef554cf7716921368ff5422ae437b62cf536a4.zip
cpython-8bef554cf7716921368ff5422ae437b62cf536a4.tar.gz
cpython-8bef554cf7716921368ff5422ae437b62cf536a4.tar.bz2
Merged revisions 75325 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r75325 | georg.brandl | 2009-10-10 23:55:11 +0200 (Sa, 10 Okt 2009) | 1 line Modernize factorisation demo (mostly augassign.) ........
Diffstat (limited to 'Demo/scripts')
-rwxr-xr-xDemo/scripts/fact.py36
1 files changed, 19 insertions, 17 deletions
diff --git a/Demo/scripts/fact.py b/Demo/scripts/fact.py
index c76474c..71fcda2 100755
--- a/Demo/scripts/fact.py
+++ b/Demo/scripts/fact.py
@@ -9,39 +9,41 @@ import sys
from math import sqrt
def fact(n):
- if n < 1: raise ValueError # fact() argument should be >= 1
- if n == 1: return [] # special case
+ if n < 1:
+ raise ValueError('fact() argument should be >= 1')
+ if n == 1:
+ return [] # special case
res = []
- # Treat even factors special, so we can use i = i+2 later
- while n%2 == 0:
+ # Treat even factors special, so we can use i += 2 later
+ while n % 2 == 0:
res.append(2)
- n = n//2
+ n //= 2
# Try odd numbers up to sqrt(n)
- limit = sqrt(float(n+1))
+ limit = sqrt(n+1)
i = 3
while i <= limit:
- if n%i == 0:
+ if n % i == 0:
res.append(i)
- n = n//i
+ n //= i
limit = sqrt(n+1)
else:
- i = i+2
+ i += 2
if n != 1:
res.append(n)
return res
def main():
if len(sys.argv) > 1:
- for arg in sys.argv[1:]:
- n = eval(arg)
- print(n, fact(n))
+ source = sys.argv[1:]
else:
+ source = iter(input, '')
+ for arg in source:
try:
- while 1:
- n = eval(input())
- print(n, fact(n))
- except EOFError:
- pass
+ n = int(arg)
+ except ValueError:
+ print(arg, 'is not an integer')
+ else:
+ print(n, fact(n))
if __name__ == "__main__":
main()