Today I was leisurely wandering about the Internet as I am wont to do, and I came across a page full of logic problems and riddles. All of the solutions were recorded on other pages which were all conveniently broken. One problem in particular caught my attention. I'll paraphrase it here.

There are 1000 men and 1000 closed doors. The first man opens each door. The second man closes each even door. The third man closes door 3, opens door 6, closes door 9, etc., and so on down the line. After all 1000 men have opened or closed doors in this fashion which doors are open?

I only thought about this for 30 seconds when I decided I had solved the problem. I didn't know the answer, but I knew how to obtain it. Here is the solution I came up with. I chose C++ for the built-in boolean type.

#include <iostream>

int main() {
    bool doors[1001]={false};
    int i,j;

    for(i=1; i<1001; i++)
        for(j=1; j<1001; j++)
            if(!(j%i))
                doors[j]=!doors[j];

    for(i=1; i<1001; i++)
        std::cout<<"Door "<<i<<": "<<(doors[i]?"open":"closed")<<"\n";
    std::cout.flush();

    return 0;
}

I'm glad that I understand how to solve problems with the computer because it's more valuable to understand how to solve problems than to know the solution to only one problem, but it saddens me that these puzzles don't hold the same magic that they did when I was eight years old.

ANSWER: Only the perfect square numbered doors will be open after all 1000 iterations (1, 4, 9, 16, etc.).


In response to the writeup below:
This was simply the first solution I came up with. This was not meant to imply that computers are a replacement for critical thinking, only that learning to program has forever changed the way I look at problem solving.