#! /usr/bin/python
# 014.py
import math
number = int(raw_input("Enter a number: "))
while number != 1:
for i in range(1, number + 1):
if (number % i) == 0 and i != 1:
number = number / i
if number == 1:
print " %d" %i
else:
print " %d*" %i,
break
结果,输入9876543210这个十位数的时候,报错:
Traceback (most recent call last):
File "./014.py", line 8, in <module>
for i in range(1, number + 1):
OverflowError: range() result has too many items
#! /usr/bin/python
# 014_1.py
import math
number = int(raw_input("Enter a number: "))
list = []
def getChildren(num):
print '*'*30
isZhishu = True
for i in range(2, int(math.sqrt(1 + num)) + 1): #多加个1
if num % i == 0 and i != num :
list.append(i)
isZhishu = False
getChildren(num / i)
break
if isZhishu:
list.append(num)
getChildren(number)
print list
Traceback (most recent call last):
File "./014_1.py", line 20, in <module >
getChildren(number)
File "./014_1.py", line 11, in getChildren
for i in range(2, int(math.sqrt(1 + num)) + 1):
MemoryError
#! /usr/bin/python
# 014_1.py
import math
number = int(raw_input("Enter a number: "))
list = []
def getChildren(num):
print '*'*30
isZhishu = True
i = 2
square = int(math.sqrt(num)) + 1
while i <= square:
if num % i == 0:
list.append(i)
isZhishu = False
getChildren(num / i)
i += 1
break
i += 1
if isZhishu:
list.append(num)
getChildren(number)
print list
同样对123124324324134334 进行操作,速度很快,得到如下结果
Enter a number: 123124324324134334
******************************
******************************
******************************
******************************
******************************
[2, 293, 313, 362107, 1853809L]