Before you suggest that this question is similar to another, read P.P.S, pls.
Simillarly to this question, I am looking to find all the non-repeating combinations of a list of factors: But beeing looking for a python solution, and also having a slighty diferent premise, I decided it's worth opening a new question.
The input to the combinations function would be of the form [2,5,3,1,5,1,11,2]
, a list where the odd entries are the primes and the even are the number of times they are present. This number would be (2^5)*3*5*(11^2), or 58080. My goal is to print all the combinations (in this case products) of the different factors.
My try is the following (b is the list where i have the primes, and div a blank list where I put the divisors(don't mind 1 as a divisor):
n=len(b)//2
a=1
if a<=n:
for i in range (n):
for g in range (1,b[2*i+1]+1):
div.append (b[2*i]**g)
a+=1
if a<=n:
for i in range (n):
for o in range (i+1,n):
for g in range(1,b[2*i+1]+1):
for h in range(1,b[2*o+1]+1):
div.append ((b[2*i]**g)*(b[2*o]**h))
This adds to the list all the combinations of at most two different prime factors, but there must be a way to continue this to numbers of n different prime factors without mannualy adding more code. But the most important is that it will not generate repeated products. If there is an answer out there please redirect me to it. Thanks in advance to all.
P.S. For example, take 60. 60 will be factored by a function (not showed here) into [2, 2, 3, 1, 5, 1]
. My desired output is all the divisors of 60, in order or not, like this [1,2,3,4,5,6,10,12,15,30,60]
All the combinations of the products of the factors. (2,2*2,3,5,2*3,2*5,2*2*3,2*2*5,3*5,2*2*3*5 (and 1, that is added to div before or after))
P.P.S. The difference to this (another) question relies on 2 things. First, and most important, the point of this question isn't finding divisors, but combinations. Divisors is just the context for it, but I would like to know for future problems how to make such iterations. Second, like I said in the comments, even if were about divisors, finding the primes and only then combining them is more efficient (for large N) than looking for numbers until the sqrt, and the refered post revolves around that (for an example why, see in comments).