algorithm
toggling lights
lighting problem
computational logic
problem-solving

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.

Practice algorithms

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 kkth pass, every kkth 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:

  1. Identify the Factors: A bulb positioned at index i will be toggled once for every factor of i . For instance, bulb at position 12 is toggled on every divisor of 12: 1, 2, 3, 4, 6, and 12 itself.
  2. 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.
  3. 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).
  4. 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:

  1. Initialize a list lights of size N with False (indicating off).
  2. Use a loop to iterate over numbers up to N\sqrt{N}.
    • For each number k in this range, calculate k^2 . • Set the k^2 -th position in the lights list to True (indicating on).
  3. Return the lights list.

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.