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

1. Member
Join Date
Sep 2012
Posts
24
Rep Power
0

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

4. Member
Join Date
Sep 2012
Posts
24
Rep Power
0

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

#### Posting Permissions

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