1. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Combinations

Can someone help find an algorithm to sum up all combinations of up to ten numbers.

For example with three random integers such as 4 5 and 7, all sums of combinations are:

1) 4
2) 5
3) 7
4) 4+5 = 9
5) 4+7 = 11
6) 5+7 = 12
7) 4+5+7 = 16

Required Output: 4 5 7 9 11 12 16  Reply With Quote

2. ## Re: Combinations

Is this a java programming question?
What have you tried? Have you done an internet search?  Reply With Quote

3. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Re: Combinations

See below, what I have been able to achieve. Like to know if there is a more streamlined approach, thanks.

public class combinatorics9{
public static void main(String[] args){
int []a = {220, 130, 120, 100, 90, 90, 60};
int [][]b = new int;
int target = 300, tot = 0, c = 0, d = 0, temp;
for(int i = 0; i < a.length - 1; ++i){
++c;
tot = tot + a[i];
if (tot <= target && tot >= target/2){
++d;
System.out.print(c + ") " + d + ") " + a[i] + " = " );
System.out.print(tot);
System.out.println(" ");
b[d] = tot;
b[d] = a[i];
}
tot = 0;
}

for(int i = 0; i < a.length - 1; ++i)
for(int j = i+1; j < a.length; ++j){
++c;
tot = tot + a[i] + a[j];
if (tot <= target && tot >= target/2){
++d;
System.out.print(c + ") " + d + ") " + a[i] + " " + a[j] + " = " );
System.out.print(tot);
System.out.println(" ");
b[d] = tot;
b[d] = a[i];
b[d] = a[j];
}
tot = 0;
}
}}  Reply With Quote

4. ## Re: Combinations

[code]
[/code]

to get highlighting and preserve formatting.

Note: single letter variable names make the code hard to read and understand. variable names should describe the data that they contain.
Please change the names to have more meaning.
Last edited by Norm; 03-19-2017 at 11:20 PM.  Reply With Quote

5. ## Re: Combinations

Use three bits and start counting: 000, 001, 010, 011, 100, 101, 110, 111; now suppose the first bit represents number 4, the second number represents 5 and the third bit represents number 7. A bit value 1 means the corresponding number is included, otherwise it isn't ...

kind regards,

Jos  Reply With Quote

6. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Re: Combinations

Thank you for your time. Going binary seems the best approach to solving this problem. If you do, kindly post the code to convert an integer into an equivalent 8 bit String or int array.  Reply With Quote

7. ## Re: Combinations Originally Posted by ashliiii Thank you for your time. Going binary seems the best approach to solving this problem. If you do, kindly post the code to convert an integer into an equivalent 8 bit String or int array.
What do you need Strings or arrays for? A simple loop with an int mask can do it all ...

kind regards,

Jos  Reply With Quote

8. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Combinations Originally Posted by JosAH What do you need Strings or arrays for? A simple loop with an int mask can do it all ...
Probably to control how the output is presented. I sorted a list of arrays based on their length. Sorting on the cardinality of the current binary value
would also work. Either way introduces inefficiencies.

Regards,
Jim  Reply With Quote

9. ## Re: Combinations

code to convert an integer into an equivalent 8 bit String or int array.
Can you give some examples of what you want to do? Given some int values, what String or array do you want to generate?

Look at the Integer class's methods.
Last edited by Norm; 03-20-2017 at 06:32 PM.  Reply With Quote

10. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Re: Combinations

There are seven loads as follows: 220 130 120 100 90 90 60 and two boats that each can hold a maximum cargo of 300. The boats cannot be overloaded. Find the maximum load that can be carried in each boat.

Solution is: 120 + 90 + 90 = 300 and 130 + 100 + 60 = 290

Write a program to find the maximum load that can be carried in each boat if there are up to ten different loads plus up to five boats of equal cargo capacities.  Reply With Quote

11. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Re: Combinations

My approach was to find the sum of combinations of the seven weights (2^7 - 1 = 1023), select the values that are less than or equal to 300 and pick the highest value. In this case it would be 300. Eliminate the loads that made up the 300 which is 120 90 90 and redo the same process with the remaining loads. Perhaps, there is a better data structure suited for this problem. Thank you for your time.  Reply With Quote

12. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Combinations

And therein lies the confusion. You came up with a plan with two parts and only asked for help with part 1. Then suggested what you would do in part 2 which no one knew anything about. It is always a good idea to explain the entire problem and "then" explain your intended approach. Sometimes you may learn that part 1 is not necessary.

Regards,
Jim  Reply With Quote

13. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Combinations

What happens if you get identical sums using different loads? Sometimes you could have the same number of loads and other times several sets of loads could be different sizes (but add up the same). So how do you arbitrate?

Regards,
Jim  Reply With Quote

14. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Re: Combinations Originally Posted by jim829 What happens if you get identical sums using different loads? Sometimes you could have the same number of loads and other times several sets of loads could be different sizes (but add up the same). So how do you arbitrate?

Regards,
Jim

Example when identical sums using different loads: 220 130 120 100 90 90 70

Then the solutions would be 120 + 90 + 90 =300 and 130 + 100 + 70 = 300.

Which ever three loads are selected first, have to omit those from the list. Using the remaining four, redo the process.  Reply With Quote

15. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Combinations Originally Posted by ashliiii Which ever three loads are selected first
Ok. I don't want to belabor this but "selected first" is simply part of the algorithm. And yours could be different than someone else's, resulting in different solutions. However, this is probably an academic discussion.

Regards,
Jim  Reply With Quote

16. Member Join Date
Mar 2017
Posts
7
Rep Power
0

## Re: Combinations Originally Posted by jim829 Ok. I don't want to belabor this but "selected first" is simply part of the algorithm. And yours could be different than someone else's, resulting in different solutions. However, this is probably an academic discussion.

Regards,
Jim
Jim, sorry if I was not responding correctly to your query of some of the dependent optimal solutions which I am sure can occur. My knowledge of the subject is somewhat limited with respect to some of you experts, that is why I was humbly hoping for possible guidance. Thank you. Ashlee  Reply With Quote

17. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Combinations

One approach would be to simply sort the results and associate the top sums with the loads that generated them, baring any other specific requirements.
Another would be to continually update the top five sums using a min or max method as required. There are probably other ways too.

Regards,
Jim  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•