Because light switches are performed in multiples of i 趟開關的時候, 會把 i
That is to say, the light bulb with the factor i will be switched on and off during this trip
It can be deduced from the above:
燈泡有奇數個因數最後的結果會是亮著的 (開關奇數次, 會是亮的)
It can be summarized as:
完全平方數的燈泡會亮著 (因為只有完全平方數有奇數個相異因數, 其他都會有兩兩成對的相異因數)
If you want to completely simulate this situation, here is the Python code:
lamps = [ False for i in range(100) ]
# print('starts', lamps)
for i in range(1, len(lamps)+1):
for idx, lamp in enumerate(lamps):
if (idx + 1) % i == 0:
lamps[idx] = not lamp
# print(i, lamps)
print(lamps.count(True))
But based on the above conclusion, you only need to know how many perfect square numbers there are in the number of light bulbs:
Additional to what @hsfzxjy said
Because light switches are performed in multiples of
i
趟開關的時候, 會把i
That is to say, the light bulb with the factor
i
will be switched on and off during this tripIt can be deduced from the above:
It can be summarized as:
If you want to completely simulate this situation, here is the Python code:
But based on the above conclusion, you only need to know how many perfect square numbers there are in the number of light bulbs:
Questions I answered: Python-QA
All perfect square numbers light up