mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
Adapt to mixed-mode arithmetic, and added a warning comment.
This commit is contained in:
parent
97dddba1bb
commit
1c8230b70d
1 changed files with 8 additions and 6 deletions
|
@ -1,7 +1,9 @@
|
||||||
#! /usr/local/python
|
#! /usr/local/python
|
||||||
|
|
||||||
# Factorize numbers, slowly.
|
# Factorize numbers.
|
||||||
# This version uses plain integers and is thus limited to 2**31-1.
|
# 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
|
import sys
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
@ -17,13 +19,13 @@ def fact(n):
|
||||||
res.append(2)
|
res.append(2)
|
||||||
n = n/2
|
n = n/2
|
||||||
# Try odd numbers up to sqrt(n)
|
# Try odd numbers up to sqrt(n)
|
||||||
limit = int(sqrt(float(n+1)))
|
limit = sqrt(n+1)
|
||||||
i = 3
|
i = 3
|
||||||
while i <= limit:
|
while i <= limit:
|
||||||
if n%i = 0:
|
if n%i = 0:
|
||||||
res.append(i)
|
res.append(i)
|
||||||
n = n/i
|
n = n/i
|
||||||
limit = int(sqrt(float(n+1)))
|
limit = sqrt(n+1)
|
||||||
else:
|
else:
|
||||||
i = i+2
|
i = i+2
|
||||||
res.append(n)
|
res.append(n)
|
||||||
|
@ -32,12 +34,12 @@ def fact(n):
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
for arg in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
n = int(eval(arg))
|
n = eval(arg)
|
||||||
print n, fact(n)
|
print n, fact(n)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
n = int(input())
|
n = input()
|
||||||
print n, fact(n)
|
print n, fact(n)
|
||||||
except EOFError:
|
except EOFError:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue