wood burning stoves
The moose likes Beginning Java and the fly likes Problem from the book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem from the book" Watch "Problem from the book" New topic

Problem from the book


Joined: Sep 04, 2001
Posts: 7
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

Joined: Oct 18, 2000
Posts: 4121

That's pretty easy....

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

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

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
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.
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;
+ firstNumber + " "
+ secondNumber + " "
+ thirdNumber);
}// end of third loop
}// end of second loop
}// end of 1st loop
"\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
[This message has been edited by Michael Fitzmaurice (edited September 11, 2001).]
[This message has been edited by Michael Fitzmaurice (edited September 11, 2001).]

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Consider Paul's rocket mass heater.
subject: Problem from the book