Ask Question

code in python Here is the problem: A new high school has just been completed. There are 100 lockers in the school and they have been numbered from 1 through 100. During recess (remember this is a fictional problem), the students decide to try an experiment. When recess is over each student will walk into the school one at a time. The first student will open all of the locker doors. The second student will close all of the locker doors with even numbers. The third student will change all of the locker doors that are multiples of 3 (change means closing lockers that are open, and opening lockers that are closed.) The fourth student will change the position of all locker doors numbered with multiples of four and so on. After 100 students have entered the school, which locker doors will be open, and why

+4
Answers (1)
  1. 16 December, 01:54
    0
    Locker 1,4,9,16,25,36,49,64,81,100 (Perfect squared lockers)

    Explanation:

    Each locker whose number is a perfect square will be open. Each locker has two possible states, open or closed. let us assume 0 means closed and 1 means open.

    A locker is initally closed, and it will only be open if we change its state an odd number of times. That is, if a locker has initial state 0, changing its state once time would make it 1 (open). Changing its state 3 times i. e.

    0->1

    1->0

    0->1

    Would make it open.

    Each locker is opened by a number that is the factor of that locker number. A factor can be defined as:

    A number c will be a factor of number a if c multiplied by a number b is a.

    i. e. c*b = a. This means b is also a factor of a.

    Each locker number that is a perfect square will only be changed an odd number of times, this is because perfect squares have only odd number of factors, where are other numbers have even number of factors.

    for example, 16 is a perfect square of 4. Its factors are

    1x16, 2x8, 4x4 but when considering factors, we only count each factor once, therefore if a number occurs twice, it is still counted as once. hence, there are a total of only 5 factors of 16, namely 1,2,4,8,16

    locker 16 is closed initially,

    Student 1 opens the locker.

    Student 2 closes the locker.

    Student 4 opens the locker.

    Student 8 closes the locker.

    Student 16 opens the locker.

    locker 8 will be changed by students 1,2,4,8, which will not change its state.

    locker 8 is closed initially.

    student 1 will open the locker

    student 2 will close the locker

    student 4 will open the locker

    student 8 will close the locker.

    Hence all the lockers that are perfectly squared will be open.

    Python Code:

    lockers = [] #Locker numbers

    lockers_s = [] #Locker states False means close, True means open.

    for i in range (1, 101):

    lockers. append (i) # initialize locker numbers

    lockers_s. append (False) #set all locker states to closed.

    for i in range (0, 100):

    for j in range (0, 100):

    if i = = 0: # Student 1. Opening all the lockers

    lockers_s[j] = True

    elif i = = 1:

    if lockers[j] % 2 = = 0: # Student 2, closing all the lockers that are multiples of 2.

    lockers_s[ lockers[j]-1 ] = False

    else:

    if lockers[j] % (i+1) = = 0: #if the locker number is a multiple of student number (i+1)

    lockers_s[ lockers[j]-1 ] = not lockers_s[ lockers[j]-1 ] # Change states.

    for i in range (0, 100):

    if lockers_s[i]:

    print (i+1, end=',') #print all the open lockers.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “code in python Here is the problem: A new high school has just been completed. There are 100 lockers in the school and they have been ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers