Open Lockers

A high school has a strange principal. On the first day, he has his students perform an odd opening day ceremony:

There are one thousand lockers and one thousand students in the school. The principal asks the first student to go to every locker and open it. Then he has the second student go to every second locker and close it. The third goes to every third locker and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth locker, and so on. After the process is completed with the thousandth student, how many lockers are open?

Note: The first locker that the n n -th student changes is the n n -th locker.


The answer is 31.

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.

13 solutions

Matthew Eyre
Jun 18, 2014

The only lockers that remain open are perfect squares (1, 4, 9, 16, etc) because they are the only numbers divisible by an odd number of whole numbers; every factor other than the number's square root is paired up with another. Thus, these lockers will be 'changed' an odd number of times, which means they will be left open. All the other numbers are divisible by an even number of factors and will consequently end up closed. So the number of open lockers is the number of perfect squares less than or equal to one thousand. These numbers are one squared, two squared, three squared, four squared, and so on, up to thirty one squared. (Thirty two squared is greater than one thousand, and therefore out of range.) So the answer is thirty one.

Maybe it's a problem of language, but I could not understand what means every second locker, every third locker and so on. Could you please explain? Thanks

Eduardo Guéron - 5 years, 4 months ago

Log in to reply

every second locker means locker #2, #4, #6, ..., #998, #1000

Suvaditya Sur - 5 years ago

Every 1st locker means every multiple of 1. Every 2nd locker means every multiple of 2. Every 3rd locker means every multiple of 3. Every 4th locker means every multiple of 4... And so on...

Christine Tan - 11 months, 1 week ago

Hey everyone! If you have comment, or a better or new way of explaining the question, it would be appreciated! Thanks.

Matthew Eyre - 6 years, 12 months ago

Log in to reply

Similar to the prison blues problem.

Anandmay Patel - 4 years, 10 months ago

... and great solution and clear explanation. Well done.

Phillip Wagoner - 2 years, 6 months ago

Matthew, that's how you do it in a nutshell. Great problem.

Sharky Kesa - 6 years, 11 months ago

pattern is open ,close,open close open close and son on isn,t it

Divyam Dalmia - 6 years, 11 months ago

Log in to reply

Nope, that's only the pattern after the 2nd student goes and toggles the lockers. After the 1000th student, the pattern is (1 for open, 0 for closed):

1001000010000001

In other words, there is an open locker, followed by a gap of of 2 n 2n closed lockers, followed by another open locker, and so on.

A Former Brilliant Member - 6 years, 11 months ago

hello i want to know how to program this in c++ perhaps, or just in detailed form would be okay. thanks! :D

Caeo Tan - 5 years, 3 months ago

Log in to reply

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream.h>
int main(){
    int i,j,a[1001]={0};int n=1000,count=0;
    for(i=1;i<=n;i++){
        for(j=i;j<=n;j=j+i){
            if(a[j]==0)a[j]=1;else a[j]=0;
        }   
    }
    for(j=1;j<=n;j++) 
        if(a[j]==1){
            cout<<j<<"\t"<<a[j]<<"\n";count++;}
    cout<<"\n\n"<<count;
    return 0;
}

Manojkumar P - 5 years, 2 months ago
Cheng Wei Chang
Jan 27, 2016

1 is visited by the first student

2 is visited by the first and second student

3 is visited by the first and third student

4 is visited by first, second and fourth student.

notice that they are visited by their factors. all factors are paired by another unless the factor is repeated which occurs when the number is a square number.

hence all numbers are visited an even number of times other than square numbers. When a locker is visited an even number of times it is closed, when it is visited an odd number of times it is opened. Largest square number smaller than 1000 is 31*31=961. So 31 lockers are opened

Excellent way of thinking about the problem! Thank you.

Oli Hohman - 5 years ago

That's just how I figured it out. The perfect squares have an odd number of factors, while any other integer number has a pair number of factors.

Flavio Moutinho - 4 years, 8 months ago

Note that any locker that is open has been opened or closed an odd number of times. This means that all open lockers must have an odd number of factors.

The number of factors any given number has can be attained the following way:

Rewrite the number as (a^b)x(c^d)x(e^f)x(g^h)x…. where a,c,e,g, etc. are the numbers prime factors.Thus, the number of as the number can have in any given factor is some number between 0 and b, inclusive, and the number of c's a number can have are between 0 and d inclusive, etc. So, therefor the number of factors any number has is (b+1)x(d+1)x(f+1)x… Clearly, since this number must be odd, all of b,d,f, etc. must therefore be even, and thus the number must be a square.

Since we know that a locker is open if and only if it is a square, and that there are 31 squares less than or equal to 1000, there must be 31 lockers open.

Rasched Haidari
Jul 1, 2014

If you examine the first 20 or so lockers and determine whether they will be open or closed, you will find a pattern. This is 1 open followed by 2 closed ,then 1 opened followed by 4 closed and 1 opened followed by 6 closed and so on. So, locker 1 is open, 2-3 are closed, 4 is open, 5-8 are closed, 9 is open, 10-15 are closed and so on. Square numbers are all open. There are 31 square numbers upto 1000 and hence 31 lockers are open.

K T
Feb 10, 2019
  • Lockers with a number with an odd number of divisors will be open in the end
  • Lockers with a number with an even number of divisors will be shut in the end
  • The number of divisors of a number N with prime representation N = p 1 m 1 × p 2 m 2 × . . . × p n m n N = p_1^{m_1}× p_2^{m_2}×... ×p_n^{m_n} is equal to ( m 1 + 1 ) ( m 2 + 1 ) . . . ( m n + 1 ) (m_1+1)(m_2+1)...(m_n+1) .

Corollary: If the prime representation of a number contains an odd power, then that number has an even number of divisors; if the prime representation of a number contains only even powers, then that number has an odd number of divisors.

Furthermore: The prime representation of N has all powers even \Leftrightarrow N is a square

Conclusion: Exactly the lockers with a square as their number will be open in the end. The largest square upto 1000 is 3 1 2 31^2 , so there will be 31 \boxed{31} lockers open

This is how I solved it too

Zoe Codrington
Aug 27, 2018

All lockers are closed at the start Student opens all lockers The 2nd opens every second locker.

Ect.

Lockers are opened/closed if the nth student is going and they have a factor of n. The trick is that for each factor, there is another factor and the pair multiply to get the original number. This means the lockers will return to closed, except...

Squares have an odd number of factors, as their square root squared is...

Well, the original number, meaning the pair of factors is really a pair of two of the same number.

This means the squares will be open at the end. There are 31 squares below 100, so that is the answer

Joe Horton
Jun 21, 2018

Being a slob, I just wrote it all out for the first 70 lockers. Noticed that, indeed, lockers 1, 4, 9, 16, etc. we’re open. Specifically, I noticed that the number of sequential closed lockers were 3, 5, 7, 9, etc. which pointed out the series. Not elegant, but effective.

Micha Shepher
Dec 11, 2017

simply count the length of the list of divisors. if it is odd, the locker will be open. only perfect squares qualify.

For any natural number n n ,

n n is a perfect square \iff n n has an odd number of positive divisors (including 1 1 and itself).

Rajat Pathak
Jul 28, 2016

a very good question

Suvaditya Sur
Jun 11, 2016

The lockers that are open have odd no of divisors => perfect squares. Therefore #perfect squares = 1000 1 + 1 = 1000 = 31 \left \lfloor \sqrt{1000} \right \rfloor - 1 + 1 = \left \lfloor \sqrt{1000} \right \rfloor = 31

That is, the kth student opened/closed the nth locker exactly when k is a factor of n. This holds for all n, so that the kth student opens/closes the nth locker exactly when k is a factor of n.

We can pair up factors of n, so that k pairs up with n/k, whenever k and n/k are different. That is, if the kth student opens the locker, then the n/k'th student closes it, and vice versa. The only time, then, that the door may be open at the end is when n/k = k for some k. This means n = k^2 is a perfect square.

So how many perfect squares are there between 1 and 1000? that is 31.

Akash Deep
Jun 20, 2014

see it is a very typical question but just we have to look into the divisors of a number. a number which has an odd number of divisors will remain opened because it is open , close , open ,............... so every number having an odd divisor will be opened. a prime no has 2 divisors so it can't be a prime numbered locker. `so first how to find the total no of divisors of a number here you can learn : https://brilliant.org/discussions/thread/divisors-of-an-integer/

in every a case of a single prime raised to an even power the no of divisors is always odd and in case where there are n primes in multiplication raised to the power which is even are also perfect squares. and yield odd no of divisors. so only perfect squares have odd no of divisors so they are the only lockers which will be open and so there are 31 perfect squares so the answer is 31.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...