## Saturday, December 19, 2015

### [LeetCode]Bulb Switcher | 书影博客

[LeetCode]Bulb Switcher | 书影博客
There are n bulbs that are initially off. You first turn on all 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 nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
Example:
```Given n = 3.

At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off].

So you should return 1, because there is only one bulb is on.```

```对于第i栈灯泡，当i的因子个数为奇数时，最终会保持点亮状态，例如9的因子为1，3，9

```0 0 0 0 0 0 0 0 0 0    0
1 1 1 1 1 1 1 1 1 1    1
1 0 1 0 1 0 1 0 1 0    2
1 0 0 0 1 1 1 0 0 0    3
1 0 0 1 1 1 1 1 0 0    4
1 0 0 1 0 1 1 1 0 1    5
1 0 0 1 0 0 1 1 0 1    6
1 0 0 1 0 0 0 1 0 1    7
1 0 0 1 0 0 0 0 0 1    8
1 0 0 1 0 0 0 0 1 1    9
1 0 0 1 0 0 0 0 1 0    10```

## Python代码：

``````class Solution(object):
def bulbSwitch(self, n):
"""
:type n: int
:rtype: int
"""
return int(math.sqrt(n))``````
http://traceformula.blogspot.com/2015/12/bulb-switcher-leetcode.html
For these types of problems, we just need to keep thinking simple, then we can solve the problem easily. In stead of imagining about all the n bulbs, we can consider 1 bulb only. Is the ith bulb on or off after rounds? We see that theith bulb is switched (on to off, or off to on) in the kth round if and only if i % k == 0. Initially the ith is on, so if i's number of divisors is odd, it will be on afterrounds, otherwise it is off.
Therefore, the importance question is: How many integer from 1 to n (inclusively) having odd numbers of divisors?
We observe that if k | i then (i/k) | i . Hence, if k == i/k, or is a square, then ihas a odd number of divisors. And the number of squares less than or equal tois floor ( sqrt (n)).
if we don't feel satisfied with the solution we have, we will eventually acquired more knowledge. ^_^. For example, in this problem, if we try to find another way to compute the result (actually it is Integer Square Root of ), we will find many methods. One of them is below credited to this stackoverflow question.
1.     public int bulbSwitch(int n) {
2.         return isqrt(n);
3.     }
4.     public int isqrt(int n){
5.         int op  = n;
6.         int res = 0;
7.         int one = 1 << 30// The second-to-top bit is set: use 1u << 14 for uint16_t type; use 1uL<<30 for uint32_t type
8.
9.
10.         // "one" starts at the highest power of four <= than the argument.
11.         while (one > op)
12.         {
13.             one >>= 2;
14.         }
15.
16.         while (one != 0)
17.         {
18.             if (op >= res + one)
19.             {
20.                 op = op - (res + one);
21.                 res = res +  2 * one;
22.             }
23.             res >>= 1;
24.             one >>= 2;
25.         }
26.         return res;
27.     }
Efficient Integer Square Root for large number.
Square Root.
Comparison between different square root methods.