VOOZH about

URL: https://www.geeksforgeeks.org/dsa/puzzle-1000-light-bulbs-switched-on-off-by-1000-persons-passing-by/

⇱ Puzzle | 1000 light bulbs switched on/off by 1000 people passing by - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Puzzle | 1000 light bulbs switched on/off by 1000 people passing by

Last Updated : 27 Apr, 2026

There are 1000 light bulbs and 1000 people. All light bulbs are initially off. Person 1 goes flipping light bulb 1, 2, 3, 4, ... person 2 then flips 2, 4, 6, 8, ... person 3 then 3, 6, 9, ... etc until all 1000 persons have done this. What is the status of light bulbs 25, 93, 576, 132, 605, 26, 45, 37, 36 after all people have flipped their respective light bulbs?

Is there a general solution to predict the status of a light bulb? How many light bulbs are on after all 1000 people have gone by?

👁 how_many_light_bulbs_are_on_after_all_1000_people_have_gone_by_

Check if you were right - full answer with solution below.

Solution: 

The process follows a clear pattern based on divisibility. We can determine the final state of each bulb by counting how many times it is toggled.

Each bulb n gets toggled once for every divisor it has (since a person k toggles bulb n only if k divides n).

  • If a bulb has an odd number of factors, it ends ON and if even number of factors, it ends OFF.
  • Therefore, a bulb’s final state depends only on whether its number of divisors is odd or even.

Example : Consider bulb 12. Its divisors are 1, 2, 3, 4, 6, and 12, so it is toggled 6 times. Since 6 is even, the bulb ends OFF.

Algorithm:

For each bulb from 1 to 1000, count all its factors. If the factor count is odd, that bulb is ON.
Below is the code implementing the above algorithm.

This program is written in O(n*sqrt(n)). 

From above, we can observe that divisors always come in pairs (e.g. 2×6 and 6×2 for 12)., giving an even count. Only perfect squares have one unpaired divisor (like √n × √n), resulting in an odd count.

Since only perfect squares have an odd number of divisors, we can skip counting and directly check for perfect squares.

Therefore, we can write an efficient code for this problem which computes in O(sqrt(n)).  

Output:

Light bulb 1 will be on
Light bulb 4 will be on
Light bulb 9 will be on
Light bulb 16 will be on
Light bulb 25 will be on
Light bulb 36 will be on
Light bulb 49 will be on
Light bulb 64 will be on
Light bulb 81 will be on
Light bulb 100 will be on
Light bulb 121 will be on
Light bulb 144 will be on
Light bulb 169 will be on
Light bulb 196 will be on
Light bulb 225 will be on
Light bulb 256 will be on
Light bulb 289 will be on
Light bulb 324 will be on
Light bulb 361 will be on
Light bulb 400 will be on
Light bulb 441 will be on
Light bulb 484 will be on
Light bulb 529 will be on
Light bulb 576 will be on
Light bulb 625 will be on
Light bulb 676 will be on
Light bulb 729 will be on
Light bulb 784 will be on
Light bulb 841 will be on
Light bulb 900 will be on
Light bulb 961 will be on
Total 31 light bulbs will be on in the end out of 1000 light bulbs.

So, Bulb n is ON if and only if n is a perfect square. Since 31² = 961 ≤ 1000 and 32² = 1024 > 1000, 31 bulbs are ON.

Hence, 25, 576 and 36 are perfect squares, so they're ON and all of the rest are OFF.

Comment
Article Tags: