beginner(first post) - Peevish Postman Problem - Programmers Heaven

Howdy, Stranger!

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


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?

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.



  • pseudocoderpseudocoder Posts: 693Member ✭✭✭

    I think you'd need to increment j by i. In order to get the pattern sequence, j would probably need to be set to 0 but skipped where you assign its new value.

    I don't know java's syntax, but in c/c++ maybe something like

    for(j = 0; j < NODOORS; j += i)
        if(j != 0) door[j] = !door[j];

    i = 1
    j = 1,2,3,4...


    and so forth

  • izthebizizthebiz chattanoogerPosts: 3Member

    Incrementing by i makes a lot of sense. My only problem now, however, is the code starts at door 54 when I print it for some reason.. so strange

    but thanks for the help!!

  • izthebizizthebiz chattanoogerPosts: 3Member

    Fixed it! My unlimited buffering was turned off.. haha

  • pseudocoderpseudocoder Posts: 693Member ✭✭✭

    No worries - always glad if I can help out a little; great work in fixing it. :D

Sign In or Register to comment.