Results 1 to 24 of 24

Thread: Combining method for math

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Combining method for math

    Hi everyone!

    I'm having a hard time trying to figure it out a new coding method for my new program using vb6. For example adding only three combination numbers to have = "9" using only numbers from 1 to 10 like for example 3+4+2=9,
    2+6+1=9, 3+1+5=9. My point is using a textbox to put the number "9" that will find combinations using numbers from 1 to 10 with a command click and to show all possible combinations in a labelbox or anything that you think is easier. I would appreciate your help for this coding if it's possible. Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Combining method for math

    You're not having trouble figuring out the code. You're (supposedly) having trouble figuring out the logic. The code is simply an implementation of the logic. The logic would be the same no matter what language you were writing the code in or even if you weren't writing any code at all. You should be putting some thought and effort into figuring out the logic first, before even considering writing any code. One of the main reasons that so many people have trouble writing code is that they have no idea what that code is supposed to do, which is the case here. Spend some time figuring out what the code has to do first. That's nothing to do with VB6 or any other programming language. Once you have a handle on the logic, if and when you have trouble implementing that logic in VB6 is when it becomes a VB6 question, at which point you can explain what the code is actually supposed to do and where you're stuck doing it.

  3. #3
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Combining method for math

    Quote Originally Posted by jmcilhinney View Post
    You're not having trouble figuring out the code. You're (supposedly) having trouble figuring out the logic. The code is simply an implementation of the logic. The logic would be the same no matter what language you were writing the code in or even if you weren't writing any code at all. You should be putting some thought and effort into figuring out the logic first, before even considering writing any code. One of the main reasons that so many people have trouble writing code is that they have no idea what that code is supposed to do, which is the case here. Spend some time figuring out what the code has to do first. That's nothing to do with VB6 or any other programming language. Once you have a handle on the logic, if and when you have trouble implementing that logic in VB6 is when it becomes a VB6 question, at which point you can explain what the code is actually supposed to do and where you're stuck doing it.
    Right on
    I was a senior systems analyst for 20 years (Large bank then biggest world telecommunication Co).
    Occasionally projects would arise, when there were not enough analysts available to run them.
    Management sometimes allocated the new projects to programmers.
    They all failed

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Combining method for math

    What about the order?

    1+2+6 would be considered the same than 2+1+6 or different?

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Combining method for math

    Well, let's see....logic?

    1-you know the 'final' result.
    2-IF you are not including zeros as any of the 'three' numbers, then you know the largest number possible would be 3 less than your desired result.
    3-Are these whole numbers, or can you have decimals? (Whole numbers is relatively easy to figure the logic for, but decimals...well, depending upon how many digits after the period (or comma, depending upon your locale), would be much more difficult to calculate (well, maybe not more difficult, but would certainly take more loops when coding).
    4-So, start with 1 (or zero if you allow them), add to it the next number (2). Take that result and subtract from your desired result to find the third number.
    Then, start with 2, add to IT the next number (3). Take THAT result and subtract from your desired result to determine the third number.
    Do this until you have until the first two numbers equal one less than the desired result.

    Now, if you do this, you will see some combinations you have saved along the way are the same, just some of the numbers will be reversed. Keep track of each 'set' and if they 'match' (that is, the same three numbers but not in the same order), then disregard that combination.

    Obviously (to me, anyway), you would have loops within loops as you do this. Seems pretty basic.

    (Eduardo...good question, but I'd wager that OP would consider the order to be relevant, IOW, 1+2+6 would be the SAME as 2+1+6.)
    Sam I am (as well as Confused at times).

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Combining method for math

    So, here's my result of a simple loop....note the third and fourth entry: they are the same numbers, but simply reversed. When that occurs, I would think you would not include it in the list.

    (Now, this sounds like a homework assignment, so I am not going to post my code, but will offer assistance on YOUR code attempts.)

    Name:  junk.JPG
Views: 387
Size:  12.0 KB
    Sam I am (as well as Confused at times).

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,871

    Re: Combining method for math

    The explanation or better the requirements are unclear.
    Are the following allowed?:
    10 = 8 + 1 + 1
    10 = 6 + 2 + 2

    Or these, which also contain the same values
    10 = 2 + 3 + 5
    10 = 2 + 5 + 3
    10 = 3 + 2 + 5
    10 = 3 + 5 + 2
    10 = 5 + 2 + 3
    10 = 5 + 3 + 2

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Combining method for math

    can you use the same number more than once?

    Examples: (4 + 4 + 1 = 9) ??? or (3 + 3 + 3 = 9) ???
    Sam I am (as well as Confused at times).

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Combining method for math

    Quote Originally Posted by SamOscarBrown View Post
    So, here's my result of a simple loop....note the third and fourth entry: they are the same numbers, but simply reversed. When that occurs, I would think you would not include it in the list.

    (Now, this sounds like a homework assignment, so I am not going to post my code, but will offer assistance on YOUR code attempts.)

    Name:  junk.JPG
Views: 387
Size:  12.0 KB
    This is what I've been trying to code. Im new to VB friend so i have limited knowledge in my progress but that code can help me a lot to make it useful in my assignment for astronomical use. I don't even know how to start coding this is something new I'm working on, if you can help me i would appreciate your help.

  10. #10
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,871

    Re: Combining method for math

    Instead of starting with coding, start with describing what you need. In detail.
    You have ignored all our questions and suggestions!

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Combining method for math

    Get a book. Read it from start to finish. If there are examples in the book, DO them.

    There are on-line books (for free, but forum rules prevent me from recommending one "I" used when I first started out). Read/order/learn.

    And like Arnie says, use pseudo-code first. Take a pen and paper (or pencil if you are prone to making errors) and 'sketch out' both in words and diagrams what you want to accomplish.

    If you don't know what pseudo-code is, you had better find the MOST BASIC of programming books and start there.

    We are not trying to discourage you, but for us to DO your stuff (homework?) for you is just wrong on several levels. You don't learn. We waste time. You don't get your homework done.

    Sammi

    EDIT...well, you WOULD get your homework done, but it wouldn't be YOU doing it...you can't go through life copy-pasting everything that comes up!!!
    Sam I am (as well as Confused at times).

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Combining method for math

    Yes, it can be used more than once as long it makes all possible combinations using numbers from 1 to 10 to get the answer from top of textbox ( whatever number is entered in textbox).

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Combining method for math

    Sam if you can only give me a piece of start code for this project i can always do the rest to finish it up and figure it out the rest and still learn as part of challenge but since i don't have nothing as evidence i can't even start homework.

  14. #14
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Combining method for math

    But if it is some kind of homework (from the school?) we would not be doing any good giving it already cooked. You won't learn anything.

    Define three variables and make three for/next for each one. Place one for/next inside the other.
    Then, inside the inner for/next, sum the three numbers and compare if they match the desired result.
    If they do, append them to a list of results.

  15. #15
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Combining method for math

    Want some code?

    Code:
    Dim n1 As Long
    Dim n2 As Long
    Dim n3 As Long
    
    For n1 = 1 To 10
    
    
    
    Next n1
    Do the rest.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Combining method for math

    Quote Originally Posted by CoderVB View Post
    Sam if you can only give me a piece of start code for this project i can always do the rest to finish it up and figure it out the rest and still learn as part of challenge but since i don't have nothing as evidence i can't even start homework.
    And still you're talking about others giving you code when you've made no attempt at all to determine the logic.
    Quote Originally Posted by CoderVB View Post
    Im new to VB friend so i have limited knowledge
    You don't need any programming experience to determine the logic so that you have limited VB knowledge is not relevant to the problem. The problem is that you aren't prepared to make the effort to come up with the logic because it's hard. If you aren't prepared to make that effort to do your own homework then you don't deserve to get the marls for it. There are plenty of people who are willing to help you if you encounter specific issues along the way but if you're not even prepared to make a start then you haven't actually encountered any issues. This is not a code issue. It's a thinking and effort issue.

  17. #17
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: Combining method for math

    Quote Originally Posted by SamOscarBrown View Post
    So, here's my result of a simple loop....note the third and fourth entry: they are the same numbers, but simply reversed. When that occurs, I would think you would not include it in the list.

    (Now, this sounds like a homework assignment, so I am not going to post my code, but will offer assistance on YOUR code attempts.)

    Name:  junk.JPG
Views: 387
Size:  12.0 KB
    I Found this results:
    input = 12
    1 + 2 + 9
    1 + 3 + 8 <--
    1 + 4 + 7 <--
    1 + 5 + 6
    2 + 3 + 7
    2 + 4 + 6 <--
    3 + 4 + 5


    It seems you have missed a few ...
    Anyway it depends on the "logic"/"rules" you use.


    other examples:


    -----------------
    9
    1 + 2 + 6
    1 + 3 + 5
    2 + 3 + 4

    -----------------
    10
    1 + 2 + 7
    1 + 3 + 6
    1 + 4 + 5
    2 + 3 + 5

    -----------------
    14
    1 + 2 + 11
    1 + 3 + 10
    1 + 4 + 9
    1 + 5 + 8
    1 + 6 + 7
    2 + 3 + 9
    2 + 4 + 8
    2 + 5 + 7
    3 + 4 + 7
    3 + 5 + 6

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Combining method for math

    Attachment 181378

    This is what I have so far but need to filter those numbers from respecting in the listbox

  19. #19

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Combining method for math

    Quote Originally Posted by Eduardo- View Post
    Want some code?

    Code:
    Dim n1 As Long
    Dim n2 As Long
    Dim n3 As Long
    
    For n1 = 1 To 10
    
    
    
    Next n1
    Do the rest.
    Name:  2021-05-10_162110.jpg
Views: 350
Size:  23.6 KB

    This my filtering but would be very long if i use this method. I'm trying get someone help me to do the short algorithm method which I'm not familiar with.

  20. #20
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: Combining method for math

    This is the code I used.
    It does not do the repetition of the same number.

    Code:
    Private Sub Form_Load()
        Dim N1%, N2%, N3%
        Dim Sum%, R$
        Sum = InputBox("Type Number", "Input", 12)
        For N1 = 1 To Sum - 1
            For N2 = N1 + 1 To Sum - 2
                For N3 = N2 + 1 To Sum - 3
                    If N1 + N2 + N3 = Sum Then
                        R = R & N1 & " + " & N2 & " + " & N3 & vbCrLf
                    End If
                Next
            Next
        Next
        Debug.Print "-----------------"
        Debug.Print Sum
        Debug.Print R
        MsgBox R
        End
    End Sub

  21. #21

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Combining method for math

    Quote Originally Posted by reexre View Post
    This is the code I used.
    It does not do the repetition of the same number.

    Code:
    Private Sub Form_Load()
        Dim N1%, N2%, N3%
        Dim Sum%, R$
        Sum = InputBox("Type Number", "Input", 12)
        For N1 = 1 To Sum - 1
            For N2 = N1 + 1 To Sum - 2
                For N3 = N2 + 1 To Sum - 3
                    If N1 + N2 + N3 = Sum Then
                        R = R & N1 & " + " & N2 & " + " & N3 & vbCrLf
                    End If
                Next
            Next
        Next
        Debug.Print "-----------------"
        Debug.Print Sum
        Debug.Print R
        MsgBox R
        End
    End Sub

    Thanks! this works for me. Just uploaded just trying to limit the numbers now I'm using is 1 to 10, when I input "15" i shouldn't see 12 and 11 on the additions but thanks i see if find the solution.

  22. #22
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Combining method for math

    Quote Originally Posted by CoderVB View Post
    Name:  2021-05-10_162110.jpg
Views: 350
Size:  23.6 KB

    This my filtering but would be very long if i use this method. I'm trying get someone help me to do the short algorithm method which I'm not familiar with.
    If this is homework, this seems like the kind of assignment that would be given for a chapter that deals with various types of loops (For loops, Do loops, etc.). Assuming that to be the case, your code above tells me that you need to thoroughly review this content. Your conclusion that this method would result in lengthy code is quite correct.

  23. #23
    Registered User
    Join Date
    Jun 2021
    Posts
    1

    Re: Combining method for math

    Can you help me with numbers incrementing every 5 seconds say from 70 to 80 and then again decrementing every 5 seconds back to 70 and showing in a label these values each five seconds. I tried coding but had difficulty while decrementing only the end result shows up in the label that is 70 can you help me with the code?

  24. #24
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Combining method for math

    1-Welcome to the Forum, sachuc1.
    2-Please start your own thread with your questions (don't hijack another's, as in this case, the solutions WILL be different)
    3-In that new thread, post your code (you post code by using the pound sign (#) icon to encapsulate the code you paste into the thread).
    4-Looking forward to seeing your new Thread.

    sammi
    Sam I am (as well as Confused at times).

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width