-1
$\begingroup$

Find all $a, b, c, d, e\in \{0, 1, 2, 4, 8\}$ such that $$a + b + c + d + e = 8.$$

Is there any method for this? I have no idea. I can just fix the limit.

  • 0
    No method ... Just think of all possible ways to get to 8 from 0,1,2,4,82017-02-05
  • 0
    There is a "dynamic programming" solution, but it's likely overkill in a problem this small.2017-02-05
  • 0
    @Michael Burr. Can you help me for "dynamic programming" solution? Thank you very much!2017-02-05
  • 1
    It seems to be a variant of the spare change problem, like http://stackoverflow.com/q/1106929/2579220. And the permutations.2017-02-05

3 Answers 3

2

This should be done through brute force or using some tricks based on the fact that almost every element in the set is a power of $2$. One could use a generating function and look at the coefficient of $x^8$ in $(1+x+x^2+x^4+x^8)^5$. A quick calculation in Maple gave me the coefficient of $x^8$ in the product is $125$.


There is also a dynamic programming solution (in the case where the numbers are nonnegative).

To do this with dynamic programming, we'll build an $6\times 9$ matrix $M$ where $M_{i,j}$ contains the number of ways to add to $j-1$ using $i-1$ variables. We know that $M_{1,1}=1$ and $M_{1,j}=0$ for $j\not=1$ since the sum of no variables is $0$. Then, for $i>1$, we can calculate $$ M_{i,j}=M_{i-1,j}+M_{i-1,j-1}+M_{i-1,j-2}+M_{i-1,j-4}+M_{i-1,j-8}. $$ Here, we assume that nonpositive subscripts on a matrix correspond to the value $0$. The result that you're looking for is in position $M_{6,9}$.


Alternatively, you can look at the different ways to get $8$ and count rearrangements. For example, you could have

  • $8+0+0+0+0$. There are $\binom{5}{1}=5$ places to place the $8$.

  • $4+4+0+0+0$. There are $\binom{5}{2}=10$ places to place the two $4$'s.

  • $4+2+2+0+0$. There are $\binom{5}{1}\binom{4}{2}=30$ places to place the $4$ and then the two $2$'s.

  • $4+2+1+1+0$. There are $\binom{5}{1}\binom{4}{1}\binom{3}{2}=60$ places to place the $4$ and then the $2$, and then the $2$ $1$'s.

  • $4+1+1+1+1$. There are $\binom{5}{1}=5$ places to place the $4$ and the remaining values are all $1$.

  • $2+2+2+2+0$. There are $\binom{5}{4}=5$ places to place the $2$'s.

  • $2+2+2+1+1$. There are $\binom{5}{3}=10$ places to place the $2$'s and the remaining spots are for $1$'s. If you add this all up, you get $125$.

  • 0
    $M_{6, 9} = 125$ ?2017-02-05
  • 0
    Yes, you get $125$ though either the dynamic programming or the generating function approaches.2017-02-05
  • 0
    Thank you very much! You have to know the Sagemath program? I want to calculate on Sagemath2017-02-05
  • 0
    I would suggest using something similar to what @mvw coded in ruby for sage.2017-02-05
  • 0
    I have difficulty in using Sagemath program. My question here http://math.stackexchange.com/questions/2129765/construction-of-formula-in-sagemath-program Can you help me? Thanks!2017-02-05
  • 0
    This is likely not the right place to get answer to sage questions. I would suggest on the sage forums themselves.2017-02-05
  • 0
    I hope that someone can help. Thank you very much!2017-02-05
1

I could not resist the brute force and told my friend Ruby about it:

def bruteforce
  count = 0
  s = [0, 1, 2, 4, 8]
  s.each do |a|
    s.each do |b|
      s.each do |c|
        s.each do |d|
          s.each do |e|
            if a+b+c+d+e == 8
              count += 1
              puts "#{count}.: #{a}+#{b}+#{c}+#{d}+#{e}"
            end
          end
        end
      end
    end
  end
  count
end

and she found $125$ combinations:

1.: 0+0+0+0+8
2.: 0+0+0+4+4
3.: 0+0+0+8+0
4.: 0+0+2+2+4
5.: 0+0+2+4+2
6.: 0+0+4+0+4
7.: 0+0+4+2+2
8.: 0+0+4+4+0
9.: 0+0+8+0+0
10.: 0+1+1+2+4
11.: 0+1+1+4+2
12.: 0+1+2+1+4
13.: 0+1+2+4+1
14.: 0+1+4+1+2
15.: 0+1+4+2+1
16.: 0+2+0+2+4
17.: 0+2+0+4+2
18.: 0+2+1+1+4
19.: 0+2+1+4+1
20.: 0+2+2+0+4
21.: 0+2+2+2+2
22.: 0+2+2+4+0
23.: 0+2+4+0+2
24.: 0+2+4+1+1
25.: 0+2+4+2+0
26.: 0+4+0+0+4
27.: 0+4+0+2+2
28.: 0+4+0+4+0
29.: 0+4+1+1+2
30.: 0+4+1+2+1
31.: 0+4+2+0+2
32.: 0+4+2+1+1
33.: 0+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1+2+1
107.: 4+0+2+0+2
108.: 4+0+2+1+1
109.: 4+0+2+2+0
110.: 4+0+4+0+0
111.: 4+1+0+1+2
112.: 4+1+0+2+1
113.: 4+1+1+0+2
114.: 4+1+1+1+1
115.: 4+1+1+2+0
116.: 4+1+2+0+1
117.: 4+1+2+1+0
118.: 4+2+0+0+2
119.: 4+2+0+1+1
120.: 4+2+0+2+0
121.: 4+2+1+0+1
122.: 4+2+1+1+0
123.: 4+2+2+0+0
124.: 4+4+0+0+0
125.: 8+0+0+0+0
  • 0
    Thank you very much! You have to know the Sagemath program? I want to calculate on Sagemath2017-02-05
0

Try approaching it as one would an R-SUM problem, with only a slight modification:

First consider the 25 element multiset with 5 copies each of 0, 1, 2, 4, 8.

Iterate over all three tuples $(a,b,c)$ and hash their sums. Then iterate over all two-tuples $(x,y)$ and check (each check takes constant time) if the value of $8-x-y$ has been hashed already. If so, then (a,b,c,x,y) satisfies $a+b+c+x+y = 8$. We don't overcount in the $(a,b,c)$s because we are hashing by tuples but we have to be slightly careful in the $(x,y)$s. In order to avoid counting $(a,b,c,x,y)$ twice, keep a boolean hash of all $(x,y)$ which led to good tuples $(a,b,c,x,y)$ and only count a tuple $(a,b,c,x,y)$ as good if $(x,y)$ hasn't been hashed yet.

The time complexity is $O(n^3)$, which is better than the $O(n^5)$ brute force approach, though in a problem this small it doesn't practically matter, this approach can be simply generalized to larger problems in which the time difference matters.