1. New to Java
Join Date
Jul 2012
Posts
5
Rep Power
0

## Problem to solve

A high school has 1000 students and 1000 lockers, one locker for each student. On the first day of school, the principal plays the following game: She asks the first student to open all the lockers. She then asks the second student to close all the even-numbered lockers. The third student is asked to check every third locker. If it is open, the student closes it; if it is closed, the student opens it. The fourth student is asked to check every fourth locker. If it is open, the student closes it; if it is closed, the student opens it. The remaining students continue this game. In general, the nth student checks every nth locker. If the locker is open, the student closes it; if it is closed, the student opens it. After all the students have taken their turns, some of the lockers are open and some are closed. Write a program that prompts the user to enter the number of lockers in a school. After the game is over, the program outputs the number of lockers and the locker numbers of the lockers that are open. Test runs your program for the following inputs: 1000, 5000, and 10000. Do you see any pattern developing for the locker numbers that are open in the output?

(Hint: Consider locker number 100. This locker is visited by student numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100. These are the positive divisors of 100. Similarly, locker number 30 is visited by student numbers 1, 2, 3, 5, 6, 10, 15, and 30. Note that if the number of positive divisors of a locker number is odd, then at the end of the game the locker is open. If the number of positive divisors of a locker number is even, then at the end of the game the locker is closed.)
Last edited by Geo; 07-19-2012 at 01:09 AM.

2. ## Re: Problem to solve

Originally Posted by Geo
please I am quite new to java. Who can give me a clue on how to solve this?
Welcome to the Java-Forums.org!

As for a clue, I would follow the instructions that were given by your instructor especially the hint. I also recommend that you have a look at "So, You Need to Write a Program but Don't Know How to Start" which can help you make sense out of these instructions.

Other useful resources include your class notes and the Java tutorials, a very comprehensive set of quite helpful tutorials.

Otherwise if you get stuck after reading the resource above, please post what you've tried and ask as specific a question as possible. Very general questions are impossible to answer other than giving general advice as I have done above.

Also a suggestion for your next question here: please try to make your question heading more informative than "Problem to solve". Of course you have a problem otherwise you wouldn't be asking a question here. In the question heading try to summarize what your actual question is sort of like a newspaper headline summarizes the article beneath it.

3. New to Java
Join Date
Jul 2012
Posts
5
Rep Power
0

## Re: Problem to solve

Thanks a lot. But I still don't know how to start in solving this...can you go through it once small and see If you can atleast assist me?

4. ## Re: Problem to solve

Originally Posted by Geo
Thanks a lot. But I still don't know how to start in solving this...can you go through it once small and see If you can atleast assist me?
But you still haven't yet told us specifically where you're stuck!

If you've gone through the link that mentioned and followed every step in that link and are still totally lost, then I'm afraid that a forum won't be able to help you. You need to contact your instructor and get some one on one help.

Otherwise if you want our help, you absolutely *must* ask a specific question. This means going through your protocol several times and asking very pointed questions on just what you don't understand. Listen, we're not going to write this code for you, but we'd be more than glad to try to clarify anything that confuses you and that helps *you* to write your code.

5. New to Java
Join Date
Jul 2012
Posts
5
Rep Power
0

## Re: Problem to solve

I didn't say you should write the program for me...Just an Idea how to start, but is alright...let me keep trying to figure it out... Thanks!

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19

## Re: Problem to solve

Point number one on that list is to understand the instructions. Do you understand them? What do you understand them to be asking you to do? (Look for the three sentences that actually ask you to do something.)

Point number 4 is where I would start - even before writing a line of code. Get yourself a pencil and a piece of paper ruled up into squares representing a hundred or so lockers and actually play around with the problem.

7. New to Java
Join Date
Jul 2012
Posts
5
Rep Power
0

## Re: Problem to solve

I've read the instructions thoroughly... Thanks a lot for your suggestions. I truly appreciate.

8. New to Java
Join Date
Jul 2012
Posts
5
Rep Power
0

## Re: Problem to solve

I'm still on it guys...

9. ## Re: Problem to solve

The hint is a complete give away: first try to figure out if a number m is a true divisor of a number n; next try to figure out how many divisors exist for a number n; after a simple check, whether or not that number is even, you can solve your problem (add a loop over all the lockers).

kind regards,

Jos

10. ## Re: Problem to solve

Please go through the Forum Rules -- particularly the third paragraph.

db

#### Posting Permissions

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