VS 2015 3 letter brute force-VBForums
Results 1 to 4 of 4

Thread: 3 letter brute force

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    2

    3 letter brute force

    Using Brute Force and Nested Loops Find all answers to AAA+BBB+CCC+DDD=ABCD where each letter stands for a digit 0-9.

    I am stuck at the solution.
    Code:
    Public Class Test
        Public Shared Sub Main()
            DIM AAA,BBB,CCC,DDD, ABCD As Integer
    	For AAA = 0 to 9
    	    For BBB = 0 to 9
    	        For CCC = 0 to 9
    	            For DDD = 0 to 9
    		If AAA+BBB+CCC+DDD=ABCD Then
    			lstAnswers.Items.Add(
                            String.Format("{0:D}, {1:D}, {2:D},{3:D}", AAA, BBB, CCC, DDD))
                End If
    
                Next DDD
            Next CCC
        Next BBB
    Next AAA
        End Sub
    End Class
    Last edited by Shaggy Hiker; Oct 4th, 2017 at 12:01 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,829

    Re: 3 letter brute force

    Welcome to the forums. I edited your post to add [CODE][/CODE] tags. You can do this by pressing the # button and pasting the code between the resulting tags, or pasting the code, then highlighting it and pressing the # button.

    As to the question, I'd say the problem is in the If statement, but I'm not quite sure what you want it to do. As it stands, you never set ABCD to anything, so it is 0. Therefore, the only time that If statement is ever true is if AAA+BBB+CCC+DDD is 0, which is only going to happen when each of those variables is 0.

    Therefore, if the goal was to have somebody put in a value for ABCD and show all the combinations that add up to that, then all you'd have to do is add something before the For loop to have the user enter a value for ABCD.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    2

    Re: 3 letter brute force

    Thanks... After much reading...Just found that I was doing this completely wrong...

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    Re: 3 letter brute force

    Your loop counters should be A, B, C and D. Your If statement would then be along the lines of:
    vb.net Code:
    1. If A * 111 + B * 111 + C * 111 + D * 111 = A * 1000 + B * 100 + C * 10 + D Then
    2.     'Match found.
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.