# How to find the numbers that sum up to a given number?

I have a list of numbers, finite, about 50 and I want to know which permutations with subsets of that set sum up to a given number. I found a formula for the number of ways but I don't know how to find the actual numbers. Can you help me?

-

1
"which permutations with subsets of that set"? – Michael Zhao Oct 4 '12 at 19:05

1
– ziyuang Oct 4 '12 at 19:14

1
So, just to be clear, for example, you have {1, 2, 3, 4, 9, 10, 13, 18}, and you want to know how many ways you can add some subset together to get 23? And, you want to know the numbers involved as well, not just the number of ways to do it? – Graphth Oct 4 '12 at 19:15
It's similar to the subset-sum problem and might be transformed to that. As far as I can tell you have understood the question correctly. I think it is a nice question because it is easy to understand. – Nick Rosencrantz Oct 4 '12 at 21:10

## 1 Answer

I assume you mean you want to find the subsets of your list whose sum is a given number $S$. You can do it with dynamic programming. Suppose your numbers $x_1, \ldots, x_n$ are positive integers and the target sum is $s$. If $A_{k,m}$ for $1 \le k \le n$, $0 \le m \le s$, is the number of subsets of $\{x_1, \ldots, x_k\}$ whose sum is $m$, we have $A_{1,0} = A_{1,x_1} = 1$, $A_{1,m} = 0$ otherwise, and for $k \ge 2$, $A_{k,m} = A_{k-1,m} + A_{k-1,m-x_k}$ if $m \ge x_k$, $A_{k-1,m}$ if $m < x_k$.
Now if $A_{k,m} > 0$ we enumerate the subsets of $\{x_1,\ldots,x_k\}$ with sum $m$ recursively as follows: if $A_{k-1,m} > 0$, first enumerate the subsets of $\{x_1,\ldots,x_{k-1}\}$ with sum $m$; then if $m \ge x_k$ and $A_{k-1,m-x_k} > 0$ enumerate the subsets of $\{x_1,\ldots,x_{k-1}\}$ with sum $m-x_k$ and adjoin $x_k$ to each.

-

Thank you for the answer. As far as I can tell you've understood the question correctly. – Nick Rosencrantz Oct 4 '12 at 21:10
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 26, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9436733722686768, "perplexity_flag": "head"}