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?

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.
// C++ implementation of above approach
#include <iostream>
using namespace std;
int findOnBulbs(int numberOfBulbs)
{
// initializing the result
int onBulbs = 0;
// to loop over all bulbs from 1 to numberOfBulbs
int bulb = 1;
// to loop over persons to check whether their person number
int person = 1;
// is a factor of light bulb number or not
for (bulb = 1; bulb <= numberOfBulbs; bulb++) {
// inner loop to find factors of given bulb
// to count the number of factors of a given bulb
int factors = 0;
for (person = 1; person * person <= numberOfBulbs; person++) {
if (bulb % person == 0) // person is a factor
{
factors++;
// bulb != person*person
if (bulb / person != person)
{
factors++;
}
}
}
// if number of factors is odd, then the
if (factors % 2 == 1)
{
// light bulb will be "on" in the end
cout << "Light bulb "
<< bulb
<< " will be on"
<< "\n";
onBulbs++;
}
}
return onBulbs;
}
// Driver program to test above function
int main()
{
// total number of light bulbs
int numberOfBulbs = 1000;
// to find number of on bulbs in
// the end after all persons have
// flipped the light bulbs
int onBulbs = findOnBulbs(numberOfBulbs);
cout << "Total "
<< onBulbs
<< " light bulbs will be on in the end out of "
<< numberOfBulbs
<< " light bulbs"
<< "\n";
return 0;
}
// Java implementation of the
// above given approach
public class GFG
{
static int findOnBulbs(int numberOfBulbs)
{
// initializing the result
int onBulbs = 0;
// to loop over all bulbs from 1 to numberOfBulbs
int bulb = 1;
// to loop over persons to check whether their person number
int person = 1;
// is a factor of light bulb number or not
for (bulb = 1; bulb <= numberOfBulbs; bulb++) {
// inner loop to find factors of given bulb
// to count the number of factors of a given bulb
int factors = 0;
for (person = 1; person * person <= numberOfBulbs; person++) {
if (bulb % person == 0) // person is a factor
{
factors++;
// bulb != person*person
if (bulb / person != person)
{
factors++;
}
}
}
// if number of factors is odd, then the
if (factors % 2 == 1)
{
// light bulb will be "on" in the end
System.out.println("Light bulb " + bulb + " will be on");
onBulbs++;
}
}
return onBulbs;
}
// Driver program to test above function
public static void main(String [] args)
{
// total number of light bulbs
int numberOfBulbs = 1000;
// to find number of on bulbs in
// the end after all persons have
// flipped the light bulbs
int onBulbs = findOnBulbs(numberOfBulbs);
System.out.println("Total " + onBulbs
+ " light bulbs will be on in the end out of "
+ numberOfBulbs + " light bulbs");
}
// This code is contributed
// by Ryuga
}
# Python3 code implementing the
# given approach
def findOnBulbs(numberOfBulbs):
# initializing the result
onBulbs = 0
# to loop over all bulbs from
# 1 to numberOfBulbs
bulb = 1
# to loop over persons to check
# whether their person number
person = 1
# Is a factor of light bulb number or not
for bulb in range(1, numberOfBulbs + 1):
# inner loop to find factors of
# given bulb to count the number
# of factors of a given bulb
factors = 0
for person in range(1, int(numberOfBulbs**(0.5)) + 1):
if bulb % person == 0: # person is a factor
factors += 1
# bulb != person*person
if bulb // person != person:
factors += 1
# if number of factors is odd, then the
if factors % 2 == 1:
# light bulb will be "on" in the end
print("Light bulb", bulb, "will be on")
onBulbs += 1
return onBulbs
# Driver Code
if __name__ == "__main__":
# total number of light bulbs
numberOfBulbs = 1000
# to find number of on bulbs in
# the end after all persons have
# flipped the light bulbs
onBulbs = findOnBulbs(numberOfBulbs)
print("Total", onBulbs, "light bulbs will",
"be on in the end out of",
numberOfBulbs, "light bulbs")
# This code is contributed
# by Rituraj Jain
// C# implementation of above approach
using System;
class GFG
{
static int findOnBulbs(int numberOfBulbs)
{
// initializing the result
int onBulbs = 0;
// to loop over all bulbs from 1 to numberOfBulbs
int bulb = 1;
// to loop over persons to check whether their person number
int person = 1;
// is a factor of light bulb number or not
for (bulb = 1; bulb <= numberOfBulbs; bulb++) {
// inner loop to find factors of given bulb
// to count the number of factors of a given bulb
int factors = 0;
for (person = 1; person * person <= numberOfBulbs; person++) {
if (bulb % person == 0) // person is a factor
{
factors++;
// bulb != person*person
if (bulb / person != person)
{
factors++;
}
}
}
// if number of factors is odd, then the
if (factors % 2 == 1)
{
// light bulb will be "on" in the end
Console.WriteLine("Light bulb " + bulb + " will be on");
onBulbs++;
}
}
return onBulbs;
}
// Driver program to test above function
public static void Main()
{
// total number of light bulbs
int numberOfBulbs = 1000;
// to find number of on bulbs in
// the end after all persons have
// flipped the light bulbs
int onBulbs = findOnBulbs(numberOfBulbs);
Console.WriteLine("Total " + onBulbs
+ " light bulbs will be on in the end out of "
+ numberOfBulbs + " light bulbs");
}
}
// This code is contributed
// by Akanksha Rai
<script>
// Javascript implementation of the
// above given approach
function findOnBulbs(numberOfBulbs)
{
// initializing the result
let onBulbs = 0;
// to loop over all bulbs from 1 to numberOfBulbs
let bulb = 1;
// to loop over persons to check whether their person number
let person = 1;
// is a factor of light bulb number or not
for (bulb = 1; bulb <= numberOfBulbs; bulb++) {
// inner loop to find factors of given bulb
// to count the number of factors of a given bulb
let factors = 0;
for (person = 1; person * person <= numberOfBulbs; person++) {
if (bulb % person == 0) // person is a factor
{
factors++;
// bulb != person*person
if (bulb / person != person)
{
factors++;
}
}
}
// if number of factors is odd, then the
if (factors % 2 == 1)
{
// light bulb will be "on" in the end
document.write("Light bulb " + bulb + " will be on<br>");
onBulbs++;
}
}
return onBulbs;
}
// Driver program to test above function
// total number of light bulbs
let numberOfBulbs = 1000;
// to find number of on bulbs in
// the end after all persons have
// flipped the light bulbs
let onBulbs = findOnBulbs(numberOfBulbs);
document.write("Total " + onBulbs
+ " light bulbs will be on in the end out of "
+ numberOfBulbs + " light bulbs");
// This code is contributed by avanitrachhadiya2155
</script>
<?php
// PHP implementation of above approach
function findOnBulbs($numberOfBulbs)
{
// initializing the result
$onBulbs = 0;
// to loop over all bulbs from
// 1 to numberOfBulbs
$bulb = 1;
// to loop over persons to check
// whether their person number
$person = 1;
// is a factor of light bulb number or not
for ($bulb = 1;
$bulb <= $numberOfBulbs; $bulb++)
{
// inner loop to find factors of given
// bulb to count the number of factors
// of a given bulb
$factors = 0;
for ($person = 1;
$person * $person <= $numberOfBulbs; $person++)
{
if ($bulb % $person == 0) // person is a factor
{
$factors++;
// bulb != person*person
if ($bulb / $person != $person)
{
$factors++;
}
}
}
// if number of factors is odd, then the
if ($factors % 2 == 1)
{
// light bulb will be "on" in the end
echo "Light bulb " . $bulb .
" will be on" ."\n";
$onBulbs++;
}
}
return $onBulbs;
}
// Driver Code
// total number of light bulbs
$numberOfBulbs = 1000;
// to find number of on bulbs in
// the end after all persons have
// flipped the light bulbs
$onBulbs = findOnBulbs($numberOfBulbs);
echo "Total " . $onBulbs . " light bulbs will " .
"be on in the end out of " . $numberOfBulbs .
" light bulbs" ."\n";
// This code is contributed by ita_c
?>
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)).
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int numberOfBulbs = 1000;
int root = sqrt(numberOfBulbs);
for (int i = 1; i < root + 1; i++)
{
cout << "Light bulb " << (i * i)
<< " will be on" << endl;
}
cout << "Total " << root
<< " light bulbs will be on in the end out of "
<< numberOfBulbs << " light bulbs" << endl;
return 0;
}
// This code is contributed by Apurvaraj
import java.io.*;
class GFG {
// Driver code
public static void main (String[] args) {
int numberOfBulbs = 1000;
int root = (int) Math.sqrt(numberOfBulbs);
for (int i = 1; i < root + 1; i++)
{
System.out.println("Light bulb " + (i * i) +" will be on");
}
System.out.println("Total " + root
+ " light bulbs will be on in the end out of "
+ numberOfBulbs + " light bulbs");
}
}
// This code is contributed b ab2127.
import math
root = int(math.sqrt(1000))
for i in range(1, root + 1):
print("Light bulb %d will be on"%(i * i))
print("""Total %d light bulbs will be on
in the end out of 1000 light bulbs"""%root)
using System;
using System.Collections.Generic;
class GFG
{
// Driver code
public static void Main(String [] args)
{
int numberOfBulbs = 1000;
int root = (int) Math.Sqrt(numberOfBulbs);
for (int i = 1; i < root + 1; i++)
{
Console.WriteLine("Light bulb " + (i * i) +" will be on");
}
Console.WriteLine("Total " + root
+ " light bulbs will be on in the end out of "
+ numberOfBulbs + " light bulbs");
}
}
// This code is contributed by 29AjayKumar
<script>
var numberOfBulbs = 1000;
var root = parseInt( Math.sqrt(numberOfBulbs));
for (i = 1; i < root + 1; i++) {
document.write("Light bulb " + (i * i) + " will be on<br/>");
}
document.write(
"Total " + root + " light bulbs will be on in the end out of "
+ numberOfBulbs + " light bulbs<br/>");
// This code is contributed by Rajput-Ji
</script>
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.