How many doors are open

2 Ansichten (letzte 30 Tage)
Phakiso Mason
Phakiso Mason am 15 Sep. 2020
Kommentiert: Stephen23 am 15 Sep. 2020
There are 1 500 people crowded in a large room. Connected to the room is a long hallway containing 1 500 closed doors. The first person in the room begins by opening every door in the long hallway. The second person goes and closes every other door. The third person goes to every third door and opens it if it is closed and closes it if it is opened. The fourth person goes to every fourth door and does the same procedure and so on. How many doors are left open after the 1 500th person has completed this cycle?
  3 Kommentare
Phakiso Mason
Phakiso Mason am 15 Sep. 2020
This is what I have done so far. The code is not running.
x = 1500;
y_correct = [1, 4, 9, 16, 25,36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444];
assert(isequal(square(x),y_correct))
Ntombikayise Bhengu
Ntombikayise Bhengu am 15 Sep. 2020
Thank you so much. It unfortunatey it didnt run.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 15 Sep. 2020
doors=zeros(1,1500);
for k=2:1500
doors(k:k:1500)=~doors(k:k:1500);
end
open=nnz(doors==0);
  2 Kommentare
Ntombikayise Bhengu
Ntombikayise Bhengu am 15 Sep. 2020
Thank you so much.
Stephen23
Stephen23 am 15 Sep. 2020
Without multiple implicit data type conversions:
doors = false(1,1500); % open=true
for k = 1:1500
doors(k:k:1500) = ~doors(k:k:1500);
end
open = nnz(doors);

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by