Bulb switcher
There is n bulbs that is initially off. You first turn the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the n-th round, you are only toggle the last bulb. Find How many bulbs is on after N rounds.
Example:
N
So you should return 1, because there are only one bulb are on.
https://leetcode.com/problems/bulb-switcher/
The title means that the bulb is off at the beginning, starting from 2 the nth wheel switch n multiples of the bulb, open the original off the bulb, closed open.
A look is math problem, make the number group to simulate definitely tle, there are large numbers, need to find the law.
Like my math clumsy player, is dead, from 1 to 10 should be able to see the law, all the remaining bulbs are square number 1, 4, 9.
Look at the explanation of the great God:
Https://leetcode.com/discuss/75014/math-solution
My understanding is that 12 can be split into 1 and 12, 2 and 6, 3 and 4, they don't touch either of them, or they hit an odd number of times, and finally it's off.
And like 10, 11 this can only hit once, and finally is off.
If it is a square number such as 9, 3 of the time off, 9 will open again, so the end is open.
1 /* * 2 * @param {number} n 3 * @return {number} 4 */ 5 var function (n) {6 return parseint (MATH.SQRT (n)); 7 };
[Leetcode] [JavaScript] Bulb switcher