Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Problem from the book

Mike_Shn
Greenhorn
Posts: 7
Hello
Does someone know an intelegent solution for the following problem:
Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a program that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on the dial range from zero to thirty-six and three numbers in sequence are needed to open the lock.
Thanks a lot

Nathan Pruett
Bartender
Posts: 4121
That's pretty easy....

Are you sure this isn't some kind of class assignment or something?

-Nate
[This message has been edited by Nathan Pruett (edited September 11, 2001).]

Michael Fitzmaurice
Ranch Hand
Posts: 168
Hi Mike
You can do this type of operation using nested for loops. The following code gives you the logic, all you need to do is change the value of the <code>MAXIMUM_VALUE</code> constant from 1 to 36.
I have used a StringBuffer rather than printing directly to the screen during the loop iterations, but if you wanted to do it that way, just replace the call to <code>Sbuf.append()</code> with a <code>System.out.println()</code> (bear in mind this will probably slow the program down even more).
It's also probably worth mentioning that when you run this code for numbers ranging from 0 to 36 rather than 0 to 1 it will dump 50653 (37 * 37 * 37) lines of output to your console, most of which you will obviously not be able to see when the program concludes. If you really want to print them to a piece of paper, I would suggest writing them out to a file instead of/as well as writing them to the screen. However, I suspect the question was just an exercise in using nested loops, so that's all I've done here.
<code><pre>
public class CombinationLock
{
// using a constant for the maximum number allowed means
// that we can change it more easily than if it was
// hardcoded into the loops themselves
private static final int MAXIMUM_VALUE= 1;

public static void main(String[] args)
{
StringBuffer sBuf = new StringBuffer();
int possibleCombinations = 0;
int firstNumber, secondNumber, thirdNumber;
for(int i = 0; i <= MAXIMUM_VALUE; i++)
{
firstNumber = i;
for(int j = 0; j <= MAXIMUM_VALUE; j++)
{
secondNumber = j;
for(int k = 0; k <= MAXIMUM_VALUE; k++)
{
thirdNumber = k;
possibleCombinations++;
sBuf.append("\n"
+ firstNumber + " "
+ secondNumber + " "
+ thirdNumber);
}// end of third loop
}// end of second loop
}// end of 1st loop
System.out.println( "POSSIBLE COMBINATIONS FOR 0 TO " + MAXIMUM_VALUE +
"\n" + sBuf.toString() );
// :NB: total possible combinations should equal size of range raised
// to the power of the number of locks (i.e. 3 in this case)
System.out.println("TOTAL OF " + possibleCombinations + " POSSIBLE COMBINATIONS");
}//end of main
}//end of class
</code></pre>
[This message has been edited by Michael Fitzmaurice (edited September 11, 2001).]
[This message has been edited by Michael Fitzmaurice (edited September 11, 2001).]