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.
Input: N = 1
Output: 1
Explanation: At first, the bulb in in off state. After the first round, the bulb will be toggled to on state.
For determining the number of bulbs that are on after N rounds, we need to consider the pattern of toggling each bulb. The idea is that a bulb will be in on state only if it has been toggled odd number of times. Now, any ith bulb will be toggled in rounds r1, r2, r3 ... where r1, r2, r3 ... are divisors of i, so we need to find only those bulbs which have odd number of divisors. So, the number of bulb which will be in on state after N rounds = count of numbers which have odd number of divisors.
To find the count of numbers which have odd number of divisors, we can say that a number has an odd number of divisors if and only if it is a perfect square. This is because divisors generally come in pairs(Eg: 1 * 12, 2 * 6, 3 * 4, 4 * 3, 6 * 2, 12 * 1 for 12). For perfect squares, one of the divisors is repeated (Eg: 1 * 9, 3 * 3, 9 * 1). Thus, the bulbs that remain on are those in positions that are perfect squares (1, 4, 9, 16, ...).The number of perfect squares less than or equal to n is the largest integer k such that k^2 <= n . This is simply the integer part of the square root of n.