If you're trying to create a puzzle where you generate 25 numbers, and there is a unique solution where 5 of them add up to a particular sum, I can think of a couple ways to do it. Neither is great, but might lead to something more workable.
The first is pretty trivial. Include the numbers 1-5, set the target to 15, and it's pretty obvious that any other number would result in a total greater than 15.
The second option is to select 20 numbers that are equivalent to 1 mod 6, then include 5 numbers that are equivalent to 0 mod 6. Then set your target to the sum of the 5 numbers that are equivalent to 0 mod 6. A little modulus arithmetic tells us that including any of the 1 mod 6 numbers would result in a number that has a remainder of 1-5, so it can't sum to the target number. We know the original creators of the puzzle didn't do this, since there are not 20 numbers equivalent to 1 mod 6 less than 100.
Next topic, if you want a formula to find certain values that sum to a given value, it can be done, but your condition of having 25 numbers to choose from is just out of reach. Consider:
Dynamic functions.xlsm |
---|
|
---|
| A | B | C | D | E |
---|
1 | Nums | | # of vals | Target | Count |
---|
2 | 67 | 0 | 20 | 331 | 5 |
---|
3 | 72 | 0 | | | |
---|
4 | 85 | 0 | | | |
---|
5 | 1 | 0 | | 1187 | |
---|
6 | 58 | 0 | | 1187 | |
---|
7 | 42 | 0 | | | |
---|
8 | 61 | 0 | | | |
---|
9 | 6 | 0 | | | |
---|
10 | 60 | 0 | | | |
---|
11 | 100 | 1 | | | |
---|
12 | 19 | 0 | | | |
---|
13 | 97 | 0 | | | |
---|
14 | 91 | 1 | | | |
---|
15 | 29 | 0 | | | |
---|
16 | 93 | 1 | | | |
---|
17 | 51 | 0 | | | |
---|
18 | 49 | 0 | | | |
---|
19 | 14 | 0 | | | |
---|
20 | 3 | 1 | | | |
---|
21 | 44 | 1 | | | |
---|
22 | 71 | | | | |
---|
23 | 90 | | | | |
---|
24 | 17 | | | | |
---|
25 | 59 | | | | |
---|
26 | 81 | | | | |
---|
|
---|
The D5 formula works in Excel 2013 and newer, the D6 formula works in Excel 365 with the new array formulas. The D5 formula fails after 20 values because (in part) it uses ROW to create an array of values, and Excel 2013+ has only 2^20 rows. The D6 formula uses SEQUENCE instead, but it too fails after 20 due to lack of resources. After all, internally it's doing matrix multiplication on an array with millions of rows and dozens of columns.
Either version figures out all 2^n possibilities, then excludes any that use more or less than 5 numbers. This is very inefficient. There is an algorithm that calculates only the combinations we want, but I don't know how to build that into a formula, it's definitely a VBA task.
Given all this, this formula might work to solve some of your puzzles. If the numbers are randomly selected, there will probably be many solutions. So try the formula, see if it works. If not, sort the input list differently and try again. If you try again and still don't get an answer, it may be that whoever created the puzzle did use some math tricks to ensure only one answer, and one or more of the needed numbers are in the bottom 5 values.
Over all, solving this kind of problem is pretty tough. See:
en.wikipedia.org
Also see this thread, especially posts 8 and 7:
Need to highlight the minimum no. of cells in the range starting from Column C till CX for each row, such that these add up to exactly the target number. Also would like to have the list of these numbers in descending order and the cell numbers of these numbers. In case same number is available...
www.mrexcel.com
That has a couple of VBA routines designed to solve the general case of this problem.
So there you are, more than you ever wanted to know.