Brain teaser, just for fun.

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. Find how many bulbs are on after n rounds.

A bulb is turned on, if and only if its number has odd numbers of factors. For example:

bulb no.: 1,2,3,4,5,6,7,8,9,10

#factors: 1,2,2,3,2,4,2,4,3,4

Factors come in pairs. For example: for 6, 1 and 6, 2 and 3 are its factors. Only square numbers have odd numbers of factors, because they have a pair of the same factors. For example: for 4, 1 and 4, 2 and 2 are its factors. So the problem reduces to count the number of square numbers below n.

The square numbers below n have the property: i^2 <= n, where i is the count we want to know. So, i <= sqrt(n).

The result/count = (int)Math.sqrt(n).

### Like this:

Like Loading...