Switching on and off bulb in JavaScript

Consider this problem: There are n bulbs that are initially off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on).

In general, for the ith round, we toggle every i bulb. For the nth round, we only toggle the last bulb. We need to find how many bulbs are on after n rounds.

Problem Example

For n = 5, here's what happens:

Round Action State [1,2,3,4,5]
Initial All off [0, 0, 0, 0, 0]
1 Toggle all [1, 1, 1, 1, 1]
2 Toggle every 2nd [1, 0, 1, 0, 1]
3 Toggle every 3rd [1, 0, 0, 0, 1]
4 Toggle every 4th [1, 0, 0, 1, 1]
5 Toggle 5th only [1, 0, 0, 1, 0]

Result: 2 bulbs are on (positions 1 and 4).

Key Insight

A bulb at position i will be toggled by all its divisors. For example, bulb 6 is toggled in rounds 1, 2, 3, and 6. A bulb ends up ON only if it's toggled an odd number of times. This happens only for perfect squares because they have an odd number of divisors.

Solution Using Mathematical Approach

const n = 5;

const findOn = (n = 1) => {
    let off = 0;
    let on = n;
    
    while (off <= on) {
        let mid = Math.floor((off + on) / 2);
        if (mid * mid > n) {
            on = mid - 1;
        } else {
            off = mid + 1;
        }
    }
    
    return Math.floor(on);
};

console.log(findOn(n));
2

Alternative Solution Using Direct Formula

const findOnDirect = (n) => {
    return Math.floor(Math.sqrt(n));
};

// Test with different values
console.log("n=1:", findOnDirect(1)); // 1 perfect square: 1
console.log("n=4:", findOnDirect(4)); // 2 perfect squares: 1, 4  
console.log("n=9:", findOnDirect(9)); // 3 perfect squares: 1, 4, 9
console.log("n=16:", findOnDirect(16)); // 4 perfect squares: 1, 4, 9, 16
n=1: 1
n=4: 2
n=9: 3
n=16: 4

Simulation Approach

const simulateBulbs = (n) => {
    let bulbs = new Array(n).fill(false);
    
    for (let round = 1; round <= n; round++) {
        for (let i = round - 1; i < n; i += round) {
            bulbs[i] = !bulbs[i]; // Toggle bulb
        }
    }
    
    return bulbs.filter(bulb => bulb).length;
};

console.log("Simulation result for n=5:", simulateBulbs(5));
console.log("Simulation result for n=10:", simulateBulbs(10));
Simulation result for n=5: 2
Simulation result for n=10: 3

How It Works

The mathematical solution uses binary search to find the largest integer whose square is ? n. This is equivalent to Math.floor(Math.sqrt(n)). Only bulbs at perfect square positions (1, 4, 9, 16, ...) remain on because perfect squares have an odd number of divisors.

Conclusion

The bulb problem has an elegant mathematical solution: the answer is simply Math.floor(Math.sqrt(n)). This works because only perfect squares have an odd number of divisors, making them the only positions that remain toggled on.

Updated on: 2026-03-15T23:19:00+05:30

751 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements