Results 1 to 4 of 4
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default I have trouble with an algorithm!! Help!!

    Hey guys, so basically i'm trying to brute force hack my pw to see how strong it is. While looking/thinking for an algorithm, i found this. Can someone explain why it works?
    Java Code:
    class asd {
    	public static void main(String[] args) {
    
    	    printWordsRec("", 2);
    	}
    
    	private static void printWordsRec(String base, int length) {
    
    	    for (char c = 'a'; c <= 'z'; c++) {
    	        if (length == 1) {
    	            System.out.println(base + c);
    	        }
    	        else {
    	            printWordsRec(base + c, length - 1);
    	        }
    	    }
    	}
    
    }
    I can almost understand:
    It goes A, then AA, then A(base) + B(c), then A(base) + C(c b/c of new for loop)
    However, when c = z, and you get A (base) + Z (c) why does it then add one to base to make it B (base) + A (c)? If the loop is over, then shouldn't it just quit? And what is making A(base) turn into B(base)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: I have trouble with an algorithm!! Help!!

    This is a recursive method. You need to fully understand how recursion works. I suggest you search the net for simple examples first and try ti understand them. Factorial is a perfect example.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: I have trouble with an algorithm!! Help!!

    Think of ordinary base ten numbers; you can enumerate all, say, three digit numbers by enumerating all two digit numbers and append either a 0 or a 1 or a 2 ... to them. Your algorithm does the same with base 26 numbers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Re: I have trouble with an algorithm!! Help!!

    Wait... I think I understand it. maybe? xD So there are 2 printWordsRec. When the second one finishes, you have A + A-Z. Because it finishes you go to the outer one, where String Base is "", and now char c = b, because you already went through it once.

Similar Threads

  1. Mph to Kph Algorithm
    By jbing109 in forum New To Java
    Replies: 4
    Last Post: 01-21-2013, 08:26 AM
  2. all i need is algorithm
    By naved in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 02:34 PM
  3. Algorithm help? :)
    By Mirix in forum New To Java
    Replies: 6
    Last Post: 05-24-2010, 02:08 AM
  4. Need some help in an algorithm
    By ea09530 in forum New To Java
    Replies: 3
    Last Post: 04-04-2010, 01:13 PM
  5. Help with an Algorithm
    By Manfizy in forum New To Java
    Replies: 22
    Last Post: 07-03-2009, 07:16 AM

Posting Permissions

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