Modifying the locker problem

Level pending

A boy enters school to see 5100 5100 lockers opened. He was tasked by his teacher to close all of the lockers. Being a very bored person, he decides to close them in a peculiar manner. First, he closes all lockers that are multiples of 2 ! 2! , then, he closes all lockers that are multiples of 3 ! 3! , however, if the locker is already closed, he opens it. He repeats this for 4 ! 4! , 5 ! 5! , 6 ! 6! and lastly 7 ! 7! . How many lockers would still be open?

My apologies, I posted this question previously with the wrong answer


The answer is 3224.

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.

1 solution

Brock Brown
Jan 5, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from math import factorial
lockers = 5100
closed = set()
# for each possible step length
for i in xrange(2,8):
    # go back to the first locker
    position = 1
    # set new step length
    step = factorial(i)
    # walk by lockers
    while position <= lockers:
        # if it's open
        if position not in closed:
            # close the locker
            closed.add(position)
        # if it's closed
        else:
            # open the locker
            closed.remove(position)
        # go to next locker
        position += step
print "Answer:", lockers - len(closed)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...