Modify the Lockers.m script. Do the following: Start with all the locker OPEN: L
ID: 3819647 • Letter: M
Question
Modify the Lockers.m script. Do the following: Start with all the locker OPEN: L = ones (1, 500); (a) Close Locker # 1. (b) Run a for-loop on k from 2 to 500. (c) In that loop, do the following: At the beginning of the loop, if L (k) is open, then print out k, and then CLOSE all locker numbers that are a integer multiple of k, all the way through 500. Thus, at the beginning, Locker # 2 should be open, and you should print out "2", close locker #2, and ALL MULTIPLES OF 2. [That would be 2, 4, 6, ..., 500.] (d) If, at the beginning of the loop, L (k) is already closed, then take no action and move on to the next value of k. (e) YOU SHOULD NEVER RE-OPEN ANY LOCKERS IN THIS SCRIPT, SO REMOVE THAT PART! YOU SHOULD ONLY BE CLOSING LOCKERS! WHAT LOCKERS REMAIN OPEN AFTER YOU EXHAUST ALL THE VALUES OF k? Explain! This method is named after a dead Greek guy. What is the name of this method?Explanation / Answer
L = ones(1,500);
L(1) = 0;
fprintf(" ");
for i = 2:500
if (L(i) == 1)
fprintf("%d ", i);
for j = i:i:500
L(j) = 0;
end
end
end
fprintf(" Lockers that are still open are : ");
for i = 2 : 500
if (L(i) == 1)
fprintf("%d ", i);
end
end
fprintf(" - ");
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
Lockers that are still open are : -
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.