Results 1 to 17 of 17
Thread: Combinations
 03192017, 08:56 PM #1Member
 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
Thank you for your time.
 03192017, 09:12 PM #2
Re: Combinations
Is this a java programming question?
What have you tried? Have you done an internet search?If you don't understand my response, don't ignore it, ask a question.
 03192017, 10:42 PM #3Member
 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[200][8];
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][0] = tot;
b[d][1] = 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][0] = tot;
b[d][1] = a[i];
b[d][2] = a[j];
}
tot = 0;
}
}}
 03192017, 11:18 PM #4
Re: Combinations
Please edit your post and wrap your code with code tags:
[code]
**YOUR CODE GOES HERE**
[/code]
to get highlighting and preserve formatting.
Also please add some comments describing the algorithm you are using.
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; 03192017 at 11:20 PM.
If you don't understand my response, don't ignore it, ask a question.
 03202017, 04:01 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 03202017, 05:17 PM #6Member
 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.
 03202017, 05:30 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
 03202017, 05:49 PM #8Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,170
 Rep Power
 12
Re: Combinations
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03202017, 05:54 PM #9
Re: Combinations
code to convert an integer into an equivalent 8 bit String or int array.
Look at the Integer class's methods.Last edited by Norm; 03202017 at 06:32 PM.
If you don't understand my response, don't ignore it, ask a question.
 03212017, 12:27 PM #10Member
 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.
 03212017, 12:33 PM #11Member
 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.
 03212017, 02:16 PM #12Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,170
 Rep Power
 12
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03212017, 02:21 PM #13Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,170
 Rep Power
 12
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03212017, 02:33 PM #14Member
 Join Date
 Mar 2017
 Posts
 7
 Rep Power
 0
Re: Combinations
 03212017, 02:53 PM #15Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,170
 Rep Power
 12
Re: Combinations
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03212017, 03:30 PM #16Member
 Join Date
 Mar 2017
 Posts
 7
 Rep Power
 0
Re: Combinations
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
 03212017, 05:36 PM #17Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,170
 Rep Power
 12
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

arrange Combinations
By silvathins in forum New To JavaReplies: 0Last Post: 05072012, 11:10 AM 
Combinations
By atomant in forum New To JavaReplies: 1Last Post: 02222012, 07:41 AM 
Combinations in Java
By Roam in forum New To JavaReplies: 12Last Post: 08252011, 10:22 PM 
Possible combinations for a n digit number???
By anishr6 in forum New To JavaReplies: 4Last Post: 05252011, 08:55 AM 
All possible combinations given a binary number
By LeanA in forum New To JavaReplies: 9Last Post: 06182010, 05:33 PM
Bookmarks