summaryrefslogtreecommitdiffstats
path: root/Demo/scripts/fact.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-12-18 13:45:17 (GMT)
committerGuido van Rossum <guido@python.org>1991-12-18 13:45:17 (GMT)
commit1c8230b70de22dd6f4d21132a7590f7a40044c44 (patch)
tree0ca03d7bb1de3673904faaf92495ecf2da2864a7 /Demo/scripts/fact.py
parent97dddba1bbb91dcf538cfae1ea5b0bf16fc0468c (diff)
downloadcpython-1c8230b70de22dd6f4d21132a7590f7a40044c44.zip
cpython-1c8230b70de22dd6f4d21132a7590f7a40044c44.tar.gz
cpython-1c8230b70de22dd6f4d21132a7590f7a40044c44.tar.bz2
Adapt to mixed-mode arithmetic, and added a warning comment.
Diffstat (limited to 'Demo/scripts/fact.py')
-rwxr-xr-xDemo/scripts/fact.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/Demo/scripts/fact.py b/Demo/scripts/fact.py
index ba75a04..6aac414 100755
--- a/Demo/scripts/fact.py
+++ b/Demo/scripts/fact.py
@@ -1,7 +1,9 @@
#! /usr/local/python
-# Factorize numbers, slowly.
-# This version uses plain integers and is thus limited to 2**31-1.
+# Factorize numbers.
+# The algorithm is not efficient, but easy to understand.
+# If there are large factors, it will take forever to find them,
+# because we try all odd numbers between 3 and sqrt(n)...
import sys
from math import sqrt
@@ -17,13 +19,13 @@ def fact(n):
res.append(2)
n = n/2
# Try odd numbers up to sqrt(n)
- limit = int(sqrt(float(n+1)))
+ limit = sqrt(n+1)
i = 3
while i <= limit:
if n%i = 0:
res.append(i)
n = n/i
- limit = int(sqrt(float(n+1)))
+ limit = sqrt(n+1)
else:
i = i+2
res.append(n)
@@ -32,12 +34,12 @@ def fact(n):
def main():
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
- n = int(eval(arg))
+ n = eval(arg)
print n, fact(n)
else:
try:
while 1:
- n = int(input())
+ n = input()
print n, fact(n)
except EOFError:
pass