Calculating all possible sums of the numbers $2^0, 2^1, \ldots, 2^{(n-1)}$

1.2k Views Asked by At

Using the simple equation $2^{n-1}$ you get values such as:

$1,2,4,8,16,32,64,128,256,$ etc.

How can I find all possible number combinations within this range? For example, the numbers $1,2,4,8$ give: \begin{align} 1 &= 1 \\ 2 &= 2 \\ 1 + 2 &= 3 \\ 4 &= 4 \\ 1 + 4 &= 5 \\ 2 + 4 &= 6 \\ 1 + 2 + 4 &= 7 \\ 8 &= 8 \\ 1 + 8 &= 9 \\ 2 + 8 &= 10 \\ 1 + 2 + 8 &= 11 \\ 4 + 8 &= 12 \\ 1 + 4 + 8 &= 13 \\ 2 + 4 + 8 &= 14 \\ 1 + 2 + 4 + 8 &= 15 \end{align}

The numbers calculable are: $1,2,3,4,5,6,7,8,9,10,11,12,13,14,15$.

Background I'm a computer programmer trying to get a unique number which represents a series of other numbers without the unique IDs being lost.

If I assign $5$ people with the id's, $1,2,4,8,16$, then in order to pass the list of them I don't need to list them all, just the mathematical equation to work it out. For example, if I wanted people $1,2,4$ then I could just pass the number $7$ and therefore it would HAVE to be people $1,2,4$ as there is no other possible combination to make this.

Please let me know if my logic is flawed somewhere!

Thanks,

Nick

2

There are 2 best solutions below

0
On BEST ANSWER

Computing the decomposition of a nonnegative integer into sums of powers of $2$ is essentially determining the binary (base-$2$)-representation of a number.

For example, we can decompose $42$ as $$42 = 32 + 8 + 2 = 1 \cdot 2^5 + 0 \cdot 2^4 + 1 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 = 0 \cdot 2^0,$$ so in binary we write $42$ in bits (the binary analogue of digits) as $$101010_2$$ (the subscript $_2$ here just reminds us that this should be read as a binary expression and not as a decimal one). Compare this decomposition with the decimal decomposition $$42 = 4 \cdot 10^1 + 2 \cdot 10^0 ;$$ we could thus write $42$ as $42_{10}$ but generally suppress a subscript $_{10}$ unless we're writing about multiple bases at once.

More generally, given the ($n + 1$) numbers $1, 2, 4, \ldots, 2^n$, the numbers expressible as sums of these are precisely the nonnegative integers with at most $n$ bits in their binary representations. (As usual, we take the empty sum to have value $0$.) These are precisely the nonnegative integers no larger than $${\underbrace{1 \cdots 1}_{n + 1}}{}_2 = 2^n + 2^{n - 1} + \cdots + 2 + 1 = 2^{n + 1} - 1.$$ By construction, this representation of any such number is unique (for the same reason that any given positive integer can only be written in decimal in a single way).

Then, in the encoding you describe, the binary representation $a_n \cdots a_0$ encodes the subset of $\{0, \ldots, n\}$ consisting precisely of the numbers $k$ for which $a_k = 1$. (In fact, this prescribes an easy algorithm for this subset from a given value of an $\texttt{integer}$-type variable.)

0
On

Every integer from $1$ to $2^n-1$ is expressible using the sums of the numbers $1,2,4,8, ... 2^{n-2}, 2^{n-1}$, and no other numbers. This can be proven using induction.