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 ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.
Return the number of bulbs that are on after n rounds.
Input: n = 3
Output: 1
Explanation: At first, the three bulbs are [off, off, off].
After the first round, the three bulbs are [on, on, on].
After the second round, the three bulbs are [on, off, on].
After the third round, the three bulbs are [on, off, off].
So you should return 1 because there is only one bulb is on.
A bulb ends up on iff it is switched an odd number of times.
Call them bulb 1 to bulb n. Bulb i is switched in round d if and only if d divides i. So bulb i ends up on if and only if it has an odd number of divisors.
Divisors come in pairs, like i=12 has divisors 1 and 12, 2 and 6, and 3 and 4. Except when i is a square, like 36 has divisors 1 and 36, 2 and 18, 3 and 12, 4 and 9, and double divisor 6. So bulb i ends up on if and only if i is a square.
So just count the square numbers.
By using some examples we can find out the number of switches for each bulb:
For each bulb i (1 to n), count how many times it is toggled (i.e., how many divisors it has). If the number of divisors is odd, the bulb is on at the end. This is a brute-force approach and is slow for large n.
classSolution {
public:int bulbSwitch(int n) {
int count =0;
for (int i =1; i <= n; ++i) {
int divisors =0;
for (int d =1; d <= i; ++d) {
if (i % d ==0) ++divisors;
}
if (divisors %2==1) ++count;
}
return count;
}
};
classSolution {
publicintbulbSwitch(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
int divisors = 0;
for (int d = 1; d <= i; d++) {
if (i % d == 0) divisors++;
}
if (divisors % 2 == 1) count++;
}
return count;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funbulbSwitch(n: Int): Int {
var count = 0for (i in1..n) {
var divisors = 0for (d in1..i) {
if (i % d ==0) divisors++ }
if (divisors % 2==1) count++ }
return count
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defbulbSwitch(self, n: int) -> int:
count =0for i in range(1, n+1):
divisors =0for d in range(1, i+1):
if i % d ==0:
divisors +=1if divisors %2==1:
count +=1return count
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnbulb_switch(n: i32) -> i32 {
letmut count =0;
for i in1..=n {
letmut divisors =0;
for d in1..=i {
if i % d ==0 { divisors +=1; }
}
if divisors %2==1 { count +=1; }
}
count
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
bulbSwitch(n: number):number {
letcount=0;
for (leti=1; i<=n; i++) {
letdivisors=0;
for (letd=1; d<=i; d++) {
if (i%d===0) divisors++;
}
if (divisors%2===1) count++;
}
returncount;
}
}
Each bulb is toggled once for every divisor it has. Divisors come in pairs, except for perfect squares (which have an odd number of divisors). Only bulbs at perfect square positions (1, 4, 9, …) are toggled an odd number of times and remain on. So, the answer is the number of perfect squares ≤ n.