Forum Discussion
Machinehandler
Feb 07, 2025Copper Contributor
Calculate which parts make up the total sum
I Have a problem with receiving only a total sum and then I need to figure out what partial sums in a long list that makes up that total. Is this somtehing that is possible for Excel to calculate, or...
Patrick2788
Feb 08, 2025Silver Contributor
This sounds like the subset sum problem. There are two ways to solve this problem in Excel: solver or create a Lambda. Last I checked Solver is limited to about 200 variables (For our example, 1 unique number = 1 variable).
With Lambda, the approach I've used is with a bitmask to produce all possible combinations. I avoid using DEC2BIN because of the function's limitations (In this example, it would only be good for 511 combinations. In my demo using only 12 integers, there are 4,095 combinations!). If you have a lot of numbers to consider then another approach would be to use a greedy algorithm.
SubsetSumλ = LAMBDA(integers, target_sum,
LET(
k, COUNT(integers),
C, SUM(COMBIN(k, SEQUENCE(k))),
bin, MOD(
BITRSHIFT(SEQUENCE(C), SEQUENCE(, k, k - 1, -1)),
2
),
M, bin * TOROW(integers),
check, BYROW(M, SUM) = target_sum,
result, FILTER(M, check, "None"),
result
)
);