-1

I am trying to find an algorithm enabling to generate the full list of possible combinations from x given numbers.

Example: possible combinations from 3 numbers (a, b,c):

a, b, c , a +b , a + c , b + c , a+b+c

Many thanks in advance for your help!

Nicola Ben
  • 10,615
  • 8
  • 41
  • 65
BIS
  • 1
  • 1

2 Answers2

0

Do you meant generate possible combination of sum of numbers?

  1. Start with an empty set s = {0}

  2. For each number a,b,c: duplicate the existing set s, add each number to the duplicated set. Add the results back to s.

Example:

s = {0}
for a:
duplicate s, s' = {0}
add a to each of s', s' = {a}
add s' back to s, s = {0,a}

for b:
duplicate s, s' = {0,a}
add b to each of s' = {b,a+b}
add s' back to s, s= {0,a,b,a+b}

for c:
dupicate s, s' = {0,a,b,a+b}
add c to each of s' = {c,a+c,b+c,a+b+c}
add s' to s, s = {0,a,b,a+b,c,a+c,b+c,a+b+c}
Joaquin
  • 2,013
  • 3
  • 14
  • 26
Kakayou
  • 598
  • 1
  • 8
  • 16
0

Treat the binary representation of the numbers from 0 to 2^x-1 as set membership. E.g., for ABC:

0 = 000 = {}
1 = 001 = {C}
2 = 010 = {B}
3 = 011 = {B,C}
4 = 100 = {A}
etc...
Dave
  • 7,460
  • 3
  • 26
  • 39