Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are 100 doors (numbered 1 through 100) and 100 students labeled with ids f

ID: 3544779 • Letter: T

Question

There are 100 doors (numbered 1 through 100) and 100 students labeled with ids from 1 to 100 waiting in a line. Initially, all the doors are closed. A student with and id x passes through all the doors that are multiples of x(i.e., student with id x goes to doors labeled with x, 2x, 3x, ... ) and changes the status of each door, i.e., opens the door if it is closed or closes the door if it is open. After all the students complete their turns, which doors remain open? How do I write an algorithm for this? Write a C program that mimics this experiment and prints the labels of doors that remain open after all students in line have passed through the doors. Moreover, it prints the number of doors that remained open.

Explanation / Answer

int y[100];


for (int z=0; z++; z<101){

y[z] = 1;

}

for (int x=1; x++; x<101){

for(int m=1; m++; (m*x)<101){

if(y[mx] == 1){

y[mx] = 0;

}

else if(y[mx] == 0){

y[mx] = 1;

}

}

}

for (int z=0; z++; z<101){

if(y[z] == 0){

printf("%d ", z);

}

}