Lists of Expressions from Lists of Numbers

User3141592

New member
Joined
Jul 13, 2022
Messages
3
I'm not sure this is quite the right place to put this, but I wasn't sure where else would be more appropriate.

I'm trying to generate all unique expressions (and their solutions, but the solutions need not [and in fact cannot] be unique) of a certain length of sums, sums of products, and products, from a list of addends and multiplicands.

The expression must always start with an addend, and the operator between any two numbers is determined by which list it comes from + for addend and * for multiplicand.


For example, say my list of addends A is 1,2,3,4,5 and my list of multiplicands M is 2,3,4, and I want all expressions of length 3.

I want:

a+a+a
a+a*m (commutatively equivalent to a*m+a)
a*m*m

for all commutatively unique arrangements of A and M. So of 1+1+2, 1+2+1, and 2+1+1 only one would appear in the final set.

This example would be pretty trivial to solve in excel; the numbers I'm actually working with are much larger making brute forcing it would take several days of sustained effort. I thought maybe someone cleverer mathematician than I would know a better way.
 
I'm not sure this is quite the right place to put this, but I wasn't sure where else would be more appropriate.

I'm trying to generate all unique expressions (and their solutions, but the solutions need not [and in fact cannot] be unique) of a certain length of sums, sums of products, and products, from a list of addends and multiplicands.

The expression must always start with an addend, and the operator between any two numbers is determined by which list it comes from + for addend and * for multiplicand.


For example, say my list of addends A is 1,2,3,4,5 and my list of multiplicands M is 2,3,4, and I want all expressions of length 3.

I want:

a+a+a
a+a*m (commutatively equivalent to a*m+a)
a*m*m

for all commutatively unique arrangements of A and M. So of 1+1+2, 1+2+1, and 2+1+1 only one would appear in the final set.

This example would be pretty trivial to solve in excel; the numbers I'm actually working with are much larger making brute forcing it would take several days of sustained effort. I thought maybe someone cleverer mathematician than I would know a better way.
Are you adept at programming? This could be relatively simply solved by a computer
 
I'm trying to generate all unique expressions (and their solutions, but the solutions need not [and in fact cannot] be unique)

What do you mean by "solutions"? Are you generating equations as well?
 
For example, say my list of addends A is 1,2,3,4,5 and my list of multiplicands M is 2,3,4, and I want all expressions of length 3.

Let's make this example simpler (so that it can be fully shown). Reducing the list of addends to 1,2,3 gives...
Code:
1+1+1 =  3           1+2*2 =  5 dup        2*2*2 =  8 dup
1+1+2 =  4           1+2*3 =  7 dup        2*2*3 = 12 dup
1+1+3 =  5           1+2*4 =  9 dup        2*2*4 = 16   
1+2+2 =  5 dup       1+3*3 = 10            2*3*3 = 18 dup
1+2+3 =  6           1+3*4 = 13            2*3*4 = 24   
1+3+3 =  7           1+4*4 = 17            2*4*4 = 32   
2+2+2 =  6 dup       2+2*2 =  6 dup        3*3*3 = 27   
2+2+3 =  7 dup       2+2*3 =  8 dup        3*3*4 = 36   
2+3+3 =  8           2+2*4 = 10 dup        3*4*4 = 48   
3+3+3 =  9           2+3*3 = 11            4*4*4 = 64   
                     2+3*4 = 14   
                     2+4*4 = 18   
                     3+2*2 =  7 dup
                     3+2*3 =  9 dup
                     3+2*4 = 11 dup
                     3+3*3 = 12   
                     3+3*4 = 15   
                     3+4*4 = 19
There are 23 unique answers if we ignore the duplicates. Is this what you wish to calculate? (Obviously for different lengths of expression and greater numbers of addends and multiplicands).

EDIT: Also, are the lists of addends/ multiplicands always consecutive? Does the list of addends always start at one? Do the multiplicands always start at 2?
 
Are you adept at programming? This could be relatively simply solved by a computer
Adept? Not really, but I can get by in C++. But I don't have a compiler, and the free one I found online had a relatively short runtime allowed. (What can you really expect from a free, web-based compiler, eh?)


Let's make this example simpler (so that it can be fully shown). Reducing the list of addends to 1,2,3 gives...
Code:
1+1+1 =  3           1+2*2 =  5 dup        2*2*2 =  8 dup
1+1+2 =  4           1+2*3 =  7 dup        2*2*3 = 12 dup
1+1+3 =  5           1+2*4 =  9 dup        2*2*4 = 16  
1+2+2 =  5 dup       1+3*3 = 10            2*3*3 = 18 dup
1+2+3 =  6           1+3*4 = 13            2*3*4 = 24  
1+3+3 =  7           1+4*4 = 17            2*4*4 = 32  
2+2+2 =  6 dup       2+2*2 =  6 dup        3*3*3 = 27  
2+2+3 =  7 dup       2+2*3 =  8 dup        3*3*4 = 36  
2+3+3 =  8           2+2*4 = 10 dup        3*4*4 = 48  
3+3+3 =  9           2+3*3 = 11            4*4*4 = 64  
                     2+3*4 = 14  
                     2+4*4 = 18  
                     3+2*2 =  7 dup
                     3+2*3 =  9 dup
                     3+2*4 = 11 dup
                     3+3*3 = 12  
                     3+3*4 = 15  
                     3+4*4 = 19
There are 23 unique answers if we ignore the duplicates. Is this what you wish to calculate? (Obviously for different lengths of expression and greater numbers of addends and multiplicands).

EDIT: Also, are the lists of addends/ multiplicands always consecutive? Does the list of addends always start at one? Do the multiplicands always start at 2?
This is exactly what I want to generate (though I don't really care about duplicates on the right hand side of the equations;).

I would prefer a general solution that doesn't necessarily need the lists to be consecutive or to always start at 1 and 2, but if those restraints significantly simplify the problem, they can be assumed.
 
This is exactly what I want to generate (though I don't really care about duplicates on the right hand side of the equations;).

I would prefer a general solution that doesn't necessarily need the lists to be consecutive or to always start at 1 and 2, but if those restraints significantly simplify the problem, they can be assumed.
Since I can't edit my post, let me say I'm already starting to see some patterns in that those lists that may simplify my process from just making gargantuan tables in excel...
 
This is exactly what I want to generate (though I don't really care about duplicates on the right hand side of the equations;).

This is good! Allowing duplicates makes it possible to calculate the number of rows in each of the three columns (of post#4). Are you familiar with stars and bars (click). Imagine three bins labelled "1", "2", and "3". Then, the sum "1+1+2" could correspond to putting two stars into the "1" bucket and one star into the "2" bucket. Now, can you figure out how to calculate the total number of rows using these hints?
 
Top