Professor:
My simple, uncommented code:
Code:
dim cellblock[100] as l
for g = 1 to 100
for c = 1 to 100 step g
cellblock[c] = iif(cellblock[c], .f., .t.)
next c
next g
for c = 1 to 100
iif(cellblock[c],trace.writeln(c+": Open"),"")
next c
and with my comments:
Code:
'All cellblocks are locked by default (.f.):
dim cellblock[100] as l
'work through each of the 100 guards:
for g = 1 to 100
'visiting each of the 100 cellblocks:
for c = 1 to 100 step g
'unlocking, if locked, and vice versa:
cellblock[c] = iif(cellblock[c], .f., .t.)
'visiting the next cellblock:
next c
'moving on to the next guard:
next g
'checking all 100 cellblocks:
for c = 1 to 100
'tracing out only the unlocked cells:
iif(cellblock[c],trace.writeln(c+": Open"),"")
next c
In the end, I had nine cellblocks open:
2: Open
5: Open
10: Open
17: Open
26: Open
37: Open
50: Open
65: Open
82: Open
Ah, but what if the guards each started, not at cellblock 1, but cellblock N:
Code:
'All cellblocks are locked by default (.f.):
dim cellblock[100] as l
'work through each of the 100 guards:
for g = 1 to 100
'visiting each of the 100 cellblocks: ***** Note that each guard starts at cellblock N *****
for c = g to 100 step g
'unlocking, if locked, and vice versa:
cellblock[c] = iif(cellblock[c], .f., .t.)
'visiting the next cellblock:
next c
'moving on to the next guard:
next g
'checking all 100 cellblocks:
for c = 1 to 100
'tracing out only the unlocked cells:
iif(cellblock[c],trace.writeln(c+": Open"),"")
next c
Now we end up with ten cellblocks open:
1: Open
4: Open
9: Open
16: Open
25: Open
36: Open
49: Open
64: Open
81: Open
100: Open
Bookmarks