Bulb Switcher
Find how many of n bulbs stay on after n rounds of switching, by counting perfect squares.
There are nnnnn
Example 1
Output: 1
Explanation: after rounds 1, 2 and 3 the bulbs are [on, on, on], [on, off, on] and [on, off, off].
Example 2
Output: 0
The Idea
Picture a hallway with ten ceiling bulbs, numbered 1 to 10, each with its own switch. Ten people walk down the hallway one after another. The first flips every switch, the second flips every 2nd switch, the third every 3rd, and the tenth flips only the switch of bulb 10. Which bulbs are lit when the tenth person has passed?
The grid below follows all ten rounds. Each column is a bulb, each row is a round, and a blue dot marks a bulb that the round toggles.
Follow the column of bulb 6 down. Rounds 1, 2, 3 and 6 toggle it, so it goes on, off, on and off again. The row labelled "times toggled" shows 4 for it, and the row labelled "at the end" shows it off. Bulb 4 has dots in rounds 1, 2 and 4 only: on, off, on. It is toggled 3 times and stays on.
So a bulb ends on exactly when it is toggled an odd number of times. Which rounds toggle bulb 6? Round rrrrr
Why do most bulbs get an even count? Divisors come in pairs that multiply to the bulb's number. For 6 the pairs are 1 × 6 and 2 × 3, four divisors in all. For 9 they are 1 × 9 and 3 × 3, and the second pair is 3 with itself, so 9 has three divisors: 1, 3 and 9.
A divisor pairs with itself only when the bulb's number is some whole number times itself. Such a number, like 1, 4 or 9, is called a perfect square. That is why the orange columns in the picture, bulbs 1, 4 and 9, are the ones that end on. The answer is the number of perfect squares from 1 to n
Solution Approach
Three things remain to settle: why flipping the bulbs round by round is the slow way, how to count the perfect squares up to n
Simulating the rounds
Why not just flip the bulbs? An array with one true or false entry per bulb, a loop over the rounds, and inside it a loop that steps through every rrnrnnnn
For ten bulbs that is the 27 dots in the picture. For a billion bulbs it is 20,877,697,634 toggles, on an array of a billion entries. The perfect squares give the same count with no array at all.
Counting the perfect squares up to n
How many perfect squares lie between 1 and nkknknnn
Turning the square root into a whole number
What does Java give for √nMath.sqrt(n)double(int)(int) Math.sqrt(10)nMath.round(Math.sqrt(3))
Could the doubleintdoublek(int) Math.sqrtkkint
The square root also keeps the code away from multiplying. A loop that counts kk * k <= nkintnInteger.MAX_VALUEn
Java Solution
The method bulbSwitch
- First, take the square root of
withn . It returns aMath.sqrt(n) , about 3.16 fordouble = 10.n - Then cast that root to
, which drops the fraction and leaves the number of perfect squares from 1 toint , 3 forn = 10. Return it as the number of bulbs that are on.n
A helper method, simulatebooleanonmainnInteger.MAX_VALUEbulbSwitchsimulaten
public class BulbSwitcher {
public static int bulbSwitch(int n) {
return (int) Math.sqrt(n); // the squares 1, 4, 9, ... up to n
}
// The slow way, used only to check the answer: flip the bulbs round by round.
static int simulate(int n) {
boolean[] on = new boolean[n + 1]; // on[b] is bulb b, from 1 to n
for (int round = 1; round <= n; round++) {
for (int b = round; b <= n; b += round) {
on[b] = !on[b];
}
}
int count = 0;
for (int b = 1; b <= n; b++) {
if (on[b]) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] tests = {3, 0, 1, 10, 99, 100, Integer.MAX_VALUE};
for (int n : tests) {
System.out.println("bulbSwitch(" + n + ") = " + bulbSwitch(n));
}
boolean same = true;
for (int n = 0; n <= 2000; n++) {
same &= bulbSwitch(n) == simulate(n);
}
System.out.println("same as simulating every round, n = 0 to 2000: " + same);
}
}
Running it prints:
bulbSwitch(3) = 1
bulbSwitch(0) = 0
bulbSwitch(1) = 1
bulbSwitch(10) = 3
bulbSwitch(99) = 9
bulbSwitch(100) = 10
bulbSwitch(2147483647) = 46340
same as simulating every round, n = 0 to 2000: true
The line bulbSwitch(10) = 3bulbSwitch(0) = 0bulbSwitch(99) = 9
For nintn
Complexity
The method does one square root and one cast, however many bulbs there are.
- Time: O(1), because the answer comes from a single
call and a cast, with no loop over the bulbs or the rounds.Math.sqrt - Space: O(1), because no array of bulbs is built and the method keeps nothing but the root it computes.
Stepping through every r