100 Doors Puzzle

Puzzle: :

You have 100 doors in a row that are all initially closed. you make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if its open, you close it). the second time you only visit every 2nd door (door #2, #4, #6). the third time, every 3rd door (door #3, #6, #9), ec, until you only visit the 100th door.

What state are the doors in after the last pass? Which are open which are closed?

Puzzle asked in: Google/Adobe/Amazon/Oracle

100 doors toggle puzzle

Puzzle Solution:

You can figure out that for any given door, say door #38, you will visit it for every divisor it has. so  has 1 & 38, 2 & 19. so on pass 1 i will open the door, pass 2 i will close it, pass 19 open, pass 38 close. For every pair of divisors the door will just end up back in its initial state. so you might think that every door will end up closed? well what about door #9. 9 has the divisors 1 & 9, 3 & 3. but 3 is repeated because 9 is a perfect square, so you will only visit door #9, on pass 1, 3, and 9… leaving it open at the end. only perfect square doors will be open at the end.

0 Thoughts on “100 Doors Puzzle

  1. Abhishek Vishal on November 7, 2014 at 5:23 pm said:

    no, u have to read the qustion for the initial condition weather the gates r closed or open,. in this qustion the gates r initially closed if gates r initially open the answer is opposite

  2. Priyanshu on September 3, 2015 at 1:32 pm said:

    We can calculate the no. of factors for each numbered door. For all those doors which have odd number of factors will be open and for all those which have even no. of factors will be closed.

    so 1,4,9,8,12,… will be open
    2,3,5,6,7,10,11,… closed

    Please let me know any suggetsions

    • only perfect squares have odd no. of factors, and every other number has even of factors.
      so the doors numbered 1,4,9,16,25,36,49,64,81,100 will be different from initial from position.

      other numbered doors will be the same as in beginning

    • 8->1,2,4,8 (even no of factors)
      12->1,2,3,4,6,12(even no of factors)

      Its a concept that only perfect squares have odd no of factors that we can keep in our memory for future use. :)

  3. guillem on April 24, 2016 at 4:47 pm said:

    I have post the c code. You can compile it with dev-c++;

    Final output is

    100100001000000100000000100000000001000000000000100000000000000100000000000000001000000000000000000

    //You have 100 doors in a row that are all initially closed.
    //you make 100 passes by the doors starting with the first door every time.
    //the first time through you visit every door and toggle the door
    //(if the door is closed, you open it, if its open, you close it).
    //the second time you only visit every 2nd door (door #2, #4, #6).
    //the third time, every 3rd door (door #3, #6, #9), ec, until you only visit the 100th door.
    //What state are the doors in after the last pass? Which are open which are closed?

    //0 means close and 1 means open
    #include

    void main (void)
    {
    int num,i,j;
    num=100;
    int ans[num];

    //initialize
    for(i=0;i<(num-1);i++)
    {
    ans[i]=0;
    }
    //iterations j=nº travel i=nºdoor
    for(j=0;j<(num-1);j++)
    {
    for(i=0;i<(num-1);i++)
    {
    //if remainder is 0
    if((i+1)%(j+1)==0)
    {
    //bit toogle
    if(ans[i]==0)
    {
    ans[i]=1;
    }
    else
    {
    ans[i]=0;
    }
    }
    }
    }

    //show by screen
    for(i=0;i<(num-1);i++)
    {
    printf("%d",ans[i]);
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation