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

• 06-24-2013, 07:14 AM
swedishfished
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?
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)
• 06-24-2013, 07:24 AM
Junky
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.
• 06-24-2013, 08:46 AM
JosAH
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
• 06-24-2013, 05:12 PM
swedishfished
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.