我正在尝试找到13195的最大质因数:
这是我得到的输出结果:
def problem3():
divisors = []
primes = []
num = 13195
for a in range(2, num):
if num % a == 0:
divisors.append(a)
print divisors #This is the list of all divisors of the number
// At this point divisors looks like:
// [5, 7, 13, 29, 35, 65, 91, 145, 203, 377, 455, 1015, 1885, 2639]
print ""
primes = divisors
for elements in divisors:
for a in range(2,elements):
if elements % a == 0:
primes.remove(elements)
print divisors
break
print primes
这是我得到的输出结果:
[5, 7, 13, 29, 65, 145, 377, 1015, 2639]
对于前四个质数,这段代码运行良好。但一旦它开始除去不是质数的数字,它似乎跳过了检查除数列表中的下一个元素,并继续向下扫描。为什么会这样呢?