Results 1 to 17 of 17

Thread: Combinations

  1. #1
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default 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.

  3. #3
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default 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;
    }
    }}

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default 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; 03-20-2017 at 12:20 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default Re: Combinations

    Quote Originally Posted by ashliiii View Post
    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
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default Re: Combinations

    Quote Originally Posted by JosAH View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default 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 07:32 PM.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default 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.

  11. #11
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default 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.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default Re: Combinations

    Quote Originally Posted by jim829 View Post
    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.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default Re: Combinations

    Quote Originally Posted by ashliiii View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    ashliiii is offline Member
    Join Date
    Mar 2017
    Posts
    7
    Rep Power
    0

    Default Re: Combinations

    Quote Originally Posted by jim829 View Post
    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

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. arrange Combinations
    By silvathins in forum New To Java
    Replies: 0
    Last Post: 05-07-2012, 12:10 PM
  2. Combinations
    By atomant in forum New To Java
    Replies: 1
    Last Post: 02-22-2012, 08:41 AM
  3. Combinations in Java
    By Roam in forum New To Java
    Replies: 12
    Last Post: 08-25-2011, 11:22 PM
  4. Possible combinations for a n- digit number???
    By anishr6 in forum New To Java
    Replies: 4
    Last Post: 05-25-2011, 09:55 AM
  5. All possible combinations given a binary number
    By LeanA in forum New To Java
    Replies: 9
    Last Post: 06-18-2010, 06:33 PM

Posting Permissions

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