Changeset 391 for python/trunk/Demo/scripts/fact.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Demo/scripts/fact.py
r2 r391 10 10 11 11 def fact(n): 12 if n < 1: raise ValueError # fact() argument should be >= 1 13 if n == 1: return [] # special case 12 if n < 1: 13 raise ValueError('fact() argument should be >= 1') 14 if n == 1: 15 return [] # special case 14 16 res = [] 15 # Treat even factors special, so we can use i = i+2 later16 while n %2 == 0:17 # Treat even factors special, so we can use i += 2 later 18 while n % 2 == 0: 17 19 res.append(2) 18 n = n//220 n //= 2 19 21 # Try odd numbers up to sqrt(n) 20 limit = sqrt( float(n+1))22 limit = sqrt(n+1) 21 23 i = 3 22 24 while i <= limit: 23 if n %i == 0:25 if n % i == 0: 24 26 res.append(i) 25 n = n//i27 n //= i 26 28 limit = sqrt(n+1) 27 29 else: 28 i = i+230 i += 2 29 31 if n != 1: 30 32 res.append(n) … … 33 35 def main(): 34 36 if len(sys.argv) > 1: 35 for arg in sys.argv[1:]: 36 n = eval(arg) 37 source = sys.argv[1:] 38 else: 39 source = iter(raw_input, '') 40 for arg in source: 41 try: 42 n = int(arg) 43 except ValueError: 44 print arg, 'is not an integer' 45 else: 37 46 print n, fact(n) 38 else:39 try:40 while 1:41 n = input()42 print n, fact(n)43 except EOFError:44 pass45 47 46 48 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.