What is the algorithm for toggling lights up to N?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In exploring algorithms for toggling lights up to N, we delve into a probing question in algorithm design and number theory. This article seeks to elucidate how a series of light toggles can operate based on simple rules and how this links to deeper mathematical concepts—specifically perfect squares.
Understanding the Problem
Imagine a series of lightbulbs, all initially turned off. Each lightbulb is toggled (switched between on and off) in a series of passes. In the first pass, every bulb is toggled. In the second, every second bulb is toggled. This pattern continues such that on the th pass, every th bulb is toggled until the Nth bulb is reached. The algorithm's task is to determine the final state (on/off) of each bulb.
Detailed Explanation
To address this problem effectively, one needs to consider the factors of each bulb index. Here’s a breakdown of the approach:
- Identify the Factors: A bulb positioned at index
iwill be toggled once for every factor ofi. For instance, bulb at position 12 is toggled on every divisor of 12: 1, 2, 3, 4, 6, and 12 itself. - Toggle Count: A crucial observation is determining when a bulb remains on after all passes. A bulb ends up in the "on" state if it is toggled an odd number of times—this means it has an odd count of divisors.
- Perfect Squares: A number has an odd number of total divisors if and only if it is a perfect square. This is because factors generally come in pairs, except when a number is a perfect square (where one factor is repeated).
- Consequence: Therefore, only bulbs at positions that are perfect squares will remain on after all toggles.
Algorithm
Given the explanation, the algorithm can be described succinctly:
• Input: An integer N
, representing the number of bulbs.
• Output: A list of bulb states (on/off).
Steps:
- Initialize a list
lightsof sizeNwithFalse(indicating off). - Use a loop to iterate over numbers up to .• For each number
kin this range, calculatek^2. • Set thek^2-th position in thelightslist toTrue(indicating on). - Return the
lightslist.
Pseudo Code
• Perfect squares up to 10 are 1, 4, and 9. • Thus, only bulbs at these positions are "on" after all passes.
Related reading
- What is the algorithm that opencv uses for finding contours?
- What is the benefit for a sort algorithm to be stable?
- What is the best 32bit hash function for short strings tag names?
- What is the best algorithm for arbitrary delimiter/escape character processing?
- What is the best algorithm for finding the closest color in an array to another color?
- What is the best algorithm for overriding GetHashCode?
- What is the best algorithm for overriding GetHashCode?
- What is the best algorithm for overriding GetHashCode?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.