返回包含乘积为x的素数的数组

时间:2017-11-07 19:45:08

标签: python algorithm list primes

我迷路了。我的功能适用于某些情况,但有些情况并非如此。例如,提供数字110019它应该返回一个数组[13,13,3,7,31],它不是......

def primeFactorization(x):
    i=2
    array=[]
    for i in range(i,x+1):
        if x % i == 0:
            j=2
            for j in range(j,x+1):
                if i % j == 0:
                i=j
            array.append(i)
            product_value=1
            for j in array:
                product_value=j*product_value
                if product_value == x:
                    array.sort()
                    return array
    return []

2 个答案:

答案 0 :(得分:3)

一些问题:

  • j上的循环没有任何意义。唯一一次if内的i == jj,因为之后i = j只会增加。此外,作业x没有任何改变,因为它们在发生时已经相等

  • 您的代码永远不能追加相同的值两次,但这可能是必要的。

  • 每次从头开始重新计算产品是一个时间的损失。在追加新因子时,您应该逐步执行此操作。在相反的方向上工作更好,并且当你找到它时,每个因素划分 i。这样你就可以避免找到非素因子。

  • 排序列表没有用,因为代码中i会增加,因此当您将def primeFactorization(x): lst = [] i = 2 while x > 1: while x % i == 0: lst.append(i) x //= i i += 1 return lst print (primeFactorization(110019)) 附加到列表中时,列表将始终保持排序。

以下是它的工作原理:

{{1}}

答案 1 :(得分:1)

问题是你的逻辑过于复杂,而你却在绊倒自己。特别是,您可以多次识别因素。因此,当您遍历嵌套循环时,因子列表中有一些因子的副本(您将其命名为array),这样该列表的产品永远不会是原始数字。因此,您永远不会对数组进行排序和返回,并退回到函数的最后一行,返回空列表。

尝试使用其中一种已知正确的方法,而不是此算法。最重要的是,当您找到一个因子并将其放入列表中时,将x除以该因子,然后继续将该因子分解。您的逻辑问题源于对原始数字的影响,然后尝试确定您找到的因子的新组成部分。

这是您的代码,其中包含一些基本的跟踪打印:

def primeFactorization(x):
    i=2
    array=[]
    for i in range(i,x+1):
        if x % i == 0:
            print x, "divisible by", i
            j = 2
            for j in range(j,x+1):
                if i % j == 0:
                    print i, "divisible by", j, "resetting"
                    i = j
            array.append(i)
            print "New factor", array
            product_value = 1
            for j in array:
                product_value = j*product_value
                if product_value == x:
                    array.sort()
                    return array
    return []

print primeFactorization(110019)

输出:

110019 divisible by 3
3 divisible by 3 resetting
New factor [3]
110019 divisible by 7
7 divisible by 7 resetting
New factor [3, 7]
110019 divisible by 13
13 divisible by 13 resetting
New factor [3, 7, 13]
110019 divisible by 21
21 divisible by 3 resetting
New factor [3, 7, 13, 3]
110019 divisible by 31
31 divisible by 31 resetting
New factor [3, 7, 13, 3, 31]
110019 divisible by 39
39 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3]
110019 divisible by 91
91 divisible by 7 resetting
New factor [3, 7, 13, 3, 31, 3, 7]
110019 divisible by 93
93 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3]
110019 divisible by 169
169 divisible by 13 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13]
110019 divisible by 217
217 divisible by 7 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7]
110019 divisible by 273
273 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3]
110019 divisible by 403
403 divisible by 13 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13]
110019 divisible by 507
507 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3]
110019 divisible by 651
651 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3]
110019 divisible by 1183
1183 divisible by 7 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7]
110019 divisible by 1209
1209 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3]
110019 divisible by 2821
2821 divisible by 7 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7]
110019 divisible by 3549
3549 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7, 3]
110019 divisible by 5239
5239 divisible by 13 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7, 3, 13]
110019 divisible by 8463
8463 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7, 3, 13, 3]
110019 divisible by 15717
15717 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7, 3, 13, 3, 3]
110019 divisible by 36673
36673 divisible by 7 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7, 3, 13, 3, 3, 7]
110019 divisible by 110019
110019 divisible by 3 resetting
New factor [3, 7, 13, 3, 31, 3, 7, 3, 13, 7, 3, 13, 3, 3, 7, 3, 7, 3, 13, 3, 3, 7, 3]
[]