LC 319: Bulb Switcher - cocoder39/coco39_LC GitHub Wiki
Supposing n = 36, which is equal to (1 * 36), (2 * 18), (3 * 12), (4 * 9) and (6 * 6). In other words, 36th bulb would be toggled at (1st round and 36th round) and (2nd round and 18th round) and (3rd round and 12nd round) and (4th round and 9th round) and (6th round only). That is to say, a bulb would be on only if it is a square number. The problem now becomes counting the number of square number from 1 to n, which is exactly sqrt(n)
int bulbSwitch(int n) {
return sqrt(n);
}