我有一个数字的质因子Python列表。我如何(pythonically)找到所有的因素是什么?有的、因子、我有一个、因素

2023-09-11 00:06:16 作者:温柔又嚣张

我工作的一个项目欧拉问题,这需要一个整数分解。我可以拿出所有的都是一个定数的因素素数的列表。算术基本定理意味着我可以使用这个列表导出的的数量每的因素。

我目前的计划是采取基地素数的列表中的每个数字和提高其功率,直到它不再是一个整数的因素,找到最大的指数为每一个素数。然后,我要加倍的黄金指数对每一个可能的组合。

因此​​,例如,为180

 鉴于:180黄金的因素:[2,3,5]
找到每个因素的最高指数:
    二分之一百八十零^ 1 = 90
    二分之一百八十零^ 2 = 45
    180°/ 2 ^ 3 = 22.5  - 不是一个整数,所以2是2的最大指数。

    三分之一百八十〇^ 1 = 60
    三分之一百八十零^ 2 = 20
    180°/ 3 ^ 3 = 6.6  - 不是一个整数,所以图2是3的最大指数。

    五分之一百八十零^ 1 = 36
    180°/ 5 ^ 2 = 7.2  - 不是一个整数,所以1是5的最大指数。
 

下一步,执行这些高达最大指数每一种组合获得的因素:

  2 ^ 0 * 3 ^ 0 * 5 ^ 0 = 1
    2 ^ 1 * 3 ^ 0 * 5 ^ 0 = 2
    2 ^ 2 * 3 ^ 0 * 5 ^ 0 = 4
    2 ^ 0 * 3 ^ 1 * 5 ^ 0 = 3
    2 ^ 1 * 3 ^ 1 * 5 ^ 0 = 6
    2 ^ 2 * 3 ^ 1 * 5 ^ 0 = 12
    2 ^ 0 * 3 ^ 2 * 5 ^ 0 = 9
    2 ^ 1 * 3 ^ 2 * 5 ^ 0 = 18
    2 ^ 2 * 3 ^ 2 * 5 ^ 0 = 36
    2 ^ 0 * 3 ^ 0 * 5 ^ 1 = 5
    2 ^ 1 * 3 ^ 0 * 5 ^ 1 = 10
    2 ^ 2 * 3 ^ 0 * 5 ^ 1 = 20
    2 ^ 0 * 3 ^ 1 * 5 ^ 1 = 15
    2 ^ 1 * 3 ^ 1 * 5 ^ 1 = 30
    2 ^ 2 * 3 ^ 1 * 5 ^ 1 = 60
    2 ^ 0 * 3 ^ 2 * 5 ^ 1 = 45
    2 ^ 1 * 3 ^ 2 * 5 ^ 1 = 90
    2 ^ 2 * 3 ^ 2 * 5 ^ 1 = 180
 

所以因素列表= [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]

python求每个 数字出现的频率 我有一个list 2 2 5 7 4 2 我想要gener

下面是code我有这么远。两个问题:首先,我不认为这是非常符合Python的。我想解决这个问题。第二,我的真正的没有一个Python化的方式做combinatoric第二步。出去丢人,我从荒谬集循环饶了你。

n是我们想要因子的数量。 listOfAllPrimes是素数高达10万美元的precalculated列表。

 高清getListOfFactors(N,listOfAllPrimes):
    MAXFACTOR = INT(的Math.sqrt(n))的+ 1个
    eligiblePrimes =过滤器(拉姆达X:X< = MAXFACTOR,listOfAllPrimes)
    listOfBasePrimes =过滤器(拉姆达X:ñ%×== 0,eligiblePrimes)

    listOfExponents = []#(我必须这样做?)
    对于x在listOfBasePrimes:
        Y = 1
        而(X **(Y + 1))%N == 0:
            Y + = 1
        listOfExponents.append(Y)
 

解决方案

而不是指数的名单,考虑简单的重复的通过次数每一个主要因素它的是的一个因素。在此之后,工作产生的 primefactors 列表与-重复,itertools.combinations不正是你所需要的 - 你只需要长2〜组合LEN(primefactors) - 1 项目包括(只是其中的组合是首要因素,他们都将是原来的号码 - 如果你想这些呢,只是用范围(1,LEN(primefactors)+ 1)而不是范围(2,LEN(primefactors))这我的主要建议是使用)。

将不会有结果的重复(例如, 6 将出现两次为倍12 ,因为后者的 primefactors [2,2,3] ),当然,他们可以在平时淘汰方式(即排序(集(结果))为例)。

要计算 primefactors listOfAllPrimes ,考虑例如:

 高清getprimefactors(N):
    primefactors = []
    primeind = 0
    P = listOfAllPrimes [primeind]
    而P< = N:
        如果n%P == 0:
            primefactors.append(p)的
            ñ// = P
        其他:
            primeind + = 1
            P = listOfAllPrimes [primeind]
    返回primefactors
 

I'm working on a Project Euler problem which requires factorization of an integer. I can come up with a list of all of the primes that are the factor of a given number. The Fundamental Theorem of Arithmetic implies that I can use this list to derive every factor of the number.

My current plan is to take each number in the list of base primes and raise its power until it is no longer an integer factor to find the maximum exponents for each prime. Then, I will multiply every possible combination of prime-exponent pairs.

So for example, for 180:

Given: prime factors of 180: [2, 3, 5]
Find maximum exponent of each  factor: 
    180 / 2^1 = 90
    180 / 2^2 = 45
    180 / 2^3 = 22.5 - not an integer, so 2 is the maximum exponent of 2.

    180 / 3^1 = 60
    180 / 3^2 = 20
    180 / 3^3 = 6.6 - not an integer, so 2 is the maximum exponent of 3.

    180 / 5^1 = 36
    180 / 5^2 = 7.2 - not an integer, so 1 is the maximum exponent of 5.

Next, do every combination of these up to the maximum exponent to get the factors:

    2^0 * 3^0 * 5^0 = 1
    2^1 * 3^0 * 5^0 = 2
    2^2 * 3^0 * 5^0 = 4
    2^0 * 3^1 * 5^0 = 3
    2^1 * 3^1 * 5^0 = 6
    2^2 * 3^1 * 5^0 = 12
    2^0 * 3^2 * 5^0 = 9
    2^1 * 3^2 * 5^0 = 18
    2^2 * 3^2 * 5^0 = 36
    2^0 * 3^0 * 5^1 = 5
    2^1 * 3^0 * 5^1 = 10
    2^2 * 3^0 * 5^1 = 20
    2^0 * 3^1 * 5^1 = 15
    2^1 * 3^1 * 5^1 = 30
    2^2 * 3^1 * 5^1 = 60
    2^0 * 3^2 * 5^1 = 45
    2^1 * 3^2 * 5^1 = 90
    2^2 * 3^2 * 5^1 = 180

So the list of factors = [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]

Here is the code I have so far. Two problems: First, I don't think it is very Pythonic at all. I'd like to fix that. Second, I really don't have a Pythonic way to do the combinatoric second step. Out of shame, I've spared you from the ridiculous set of loops.

n is the number we want to factor. listOfAllPrimes is a precalculated list of the primes up to 10 million.

def getListOfFactors(n, listOfAllPrimes):
    maxFactor = int(math.sqrt(n)) + 1
    eligiblePrimes = filter(lambda x: x <= maxFactor, listOfAllPrimes)
    listOfBasePrimes = filter(lambda x: n % x ==0, eligiblePrimes)

    listOfExponents = [] #(do I have to do this?)
    for x in listOfBasePrimes:
        y = 1
        while (x**(y+1)) % n == 0:
            y += 1
        listOfExponents.append(y)

解决方案

Instead of a list of exponents, consider simply repeating each prime factor by the number of times it is a factor. After that, working on the resulting primefactors list-with-repetitions, itertools.combinations does just what you need -- you'll just require the combinations of length 2 to len(primefactors) - 1 items included (the combinations of just one are the prime factors, that of all of them will be the original number -- if you want those too, just use range(1, len(primefactors) + 1) instead of the range(2, len(primefactors)) which my main suggestion would use).

There will be repetitions in the results (e.g., 6 will appear twice as a factor of 12, since the latter's primefactors will be [2, 2, 3]) and they can of course be weeded out in the usual ways (i.e. sorted(set(results)) for example).

To compute primefactors given listOfAllPrimes, consider for example:

def getprimefactors(n):
    primefactors = []
    primeind = 0
    p = listOfAllPrimes[primeind]
    while p <= n:
        if n % p == 0:
            primefactors.append(p)
            n //= p
        else:
            primeind += 1
            p = listOfAllPrimes[primeind]
    return primefactors