Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

beginner(first post) - Peevish Postman Problem

izthebizizthebiz chattanoogerPosts: 3Member

Here's the given problem and code:

" 3) British puzzle maker H. E. Dudeney concocted an interesting puzzle about a bored postman called the “Peevish Postman Problem”. According to Dudeney, the postman worked in a small post office with consecutive letter boxes numbered 1 to 100. Each box was equipped with a door that could be opened and closed. Late one evening the postman made a “pass” through the boxes and opened every door. Still bored, he walked back to the beginning and made a second pass, this time visiting boxes 2, 4, 6, ..., 100. Since those doors were now open, he closed them. On the third pass he visited boxes 3, 6, 9, 12, ..., 99 and if a door was open he closed it, and if the door was closed he opened it. He continued to make passes through the boxes and always followed the same rule: On each pass 'i' from 1 to 100, he visited only boxes that were multiples of 'i', ...and changed the state of each door he visited. After making 100 passes at the doors, he surveyed the results and was surprised by the pattern of doors that he saw.

The code below uses a Boolean array to represent the doors. A true value in the array represents an open door, and a false value represents a closed one. You will have to write two nested loops in order to manipulate the array as described above. The inner loop will control the door number visited on a single pass, and the outer loop will control the number of passes. Print the state of each door after the 100th pass.

The puzzle was conceived as a paper and pencil entertainment. Can you explain the pattern of doors?

Hint: Because the doors are numbered starting at one, we will waste the first position in the array. In this case, the default value will be set to false. By ignoring the first position, the door numbers match their index positions in the array. "

public class Peevish
    public static void main(String[] args)
        boolean[] door;
        final int NODOORS = 101; // We will not use door[0]
        final boolean OPEN = true;
        final boolean CLOSED = false;
        // Initialize the doors
        door = new boolean[NODOORS];
        for (int i = 0; i < NODOORS; i++)
            door[i] = CLOSED;
        // Print the state of each door (only doors 1-100)
        for (int i = 1; i < NODOORS; i++)
            if (door[i] == true)
                System.out.println("Door " + i + " is open.");
                System.out.println("Door " + i + " is closed.");
        // My code here

        for(int i = 1; i <= 100; i++)
            for (int j = 1; j <= 100; j++)


pastebin link:

I have written what's under the comment "My code here". I know I need to use multiples, but I'm not sure how to go about it. Also, I tried debugging and walking through the code line by line, but I'm still having a hard time wrapping my mind around this one.

Thanks in advance for any and all help. -iz



Sign In or Register to comment.