Hotel Puzzle

A HOTEL with 1000 rooms has 1000 waiters. Each waiter and room is given a number from 1-1000.

Initially, waiter with number 1 goes and closes doors of all rooms.
After that waiter with number 2 goes and changes the state of doors corresponding to multiples of that 2.
After that waiter number 3 goes and changes the state of doors corresponding to multiples of 3.
So on and so forth

Find the number of doors that are open after the 1000th waiter has returned .

NOTE: Changes the state of door means that opened doors are closed and closed doors are opened.


The answer is 969.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Saket Sharma
Sep 27, 2014

Only the room numbers which are perfect square will be left close! There are 31 perfect squares (natural numbers) less than 1000. Hence, 1000 - 31 = 969 will be left open.

Detailed explanation: A room is left close if number of its divisors (including 1 and the number itself) is odd. For example, take room no. 4: Divisors - 1 ( -> close), 2 ( -> open), 4 ( ->close). Hence, finally close.

Now a natural number n n can be written in the form of

n = p a q b r c . . . . n = p^{a}*q^{b}*r^{c}....

where, p p , q q , r r , . . . ... are prime factors of n n and a a , b b , c c , . . . ... are natural numbers.

Number of divisors,

D ( n ) = ( a + 1 ) ( b + 1 ) ( c + 1 ) . . . . D(n) = (a+1)(b+1)(c+1).... .

D ( n ) D(n) can be odd only if each of a , b , c , . . . a, b, c, ... is even. This in turn implies that number n n is a perfect square.

A perfect explanation given by Saket Sharma

Anupam Khandelwal - 6 years, 8 months ago
Jonathan Hsu
Apr 29, 2015

First, I asked a question to myself: What makes a door closed? The answer is simple: It has to be opened and closed an odd number of times. The only numbers that follow this condition are perfect squares. Hence, 31 are closed, which means 1000-31=169 are open.

Tony Han
Jan 7, 2019

Quick solution using numpy :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import numpy as np

doors = np.array([1 for i in range(1000)])

for n in range(1, 1001):
    # where 1 is a open door, and 0 is closed
    state_change = np.array([1 if i % n == 0 else 0 for i in range(1,1001)])
    doors = (doors + state_change) % 2

print(sum(doors))

Dilip Singh
Nov 24, 2014

int openDoors() { int doorState[1000]; int i = 0; int waiter = 2; int oDoors = 0;

while(i < 1000)
{
    doorState[i++] = 0;
}

while(waiter <= 1000)
{
    i = 0;
    while(i < 1000)
    {
        if( ((i + 1) % waiter) == 0)
        {
            if(doorState[i] > 0)
            {
                doorState[i] = 0;
            }
            else
            {
                doorState[i] = 1;
            }
        }
        i = i + 1;
    }
    waiter = waiter + 1;
}

i = 0;

while(i < 1000)
{
    if(doorState[i++])
    {
        oDoors = oDoors + 1;
    }
}
printf("Open doors = %d", oDoors);

}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...