Lamps After Divisor Sweeps
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Count how many of n switches end up on after sweeping by every step size, which tests the divisor-parity and perfect-square insight.
A corridor has n lamps in a row, numbered 1 through n. All lamps start switched off.
You make n sweeps along the corridor. On sweep k (for k = 1, 2, ..., n), you flip the state of every lamp whose number is a multiple of k. Flipping turns an off lamp on and an on lamp off.
After all n sweeps are done, return how many lamps are on.
Implement a function that takes n and returns this count as an integer. If n is 0, there are no lamps and the answer is 0.
A direct simulation is too slow for large n, so look for the pattern in which lamps end up on.
Constraints
0 <= n <= 10^18nis an integer.- The return value is a single integer.
Examples
Example 1
Input: count_lamps_on(3)
Output: 1
Example 2
Input: count_lamps_on(10)
Output: 3
Example 3
Input: count_lamps_on(25)
Output: 5
Hints
Hint 1
Lamp j is flipped once for each sweep k that divides j. Its final state depends only on whether its number of divisors is odd or even.
Hint 2
Divisors normally pair up as (d, j/d). When does a divisor fail to have a distinct partner?
Hint 3
Only perfect squares have an odd number of divisors, so count the perfect squares in 1..n. Use an exact integer square root instead of floating-point sqrt to avoid precision errors for large n.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Lamp j is flipped once for every sweep number k that divides j. It ends up on exactly when j has an odd number of divisors, since it starts off.
Divisors of j come in pairs (d, j / d) with d != j / d, and each pair adds two to the count. The only way to get an odd total is for some divisor to be paired with itself, which happens when d * d = j, that is, when j is a perfect square. So lamp j ends on if and only if j is a perfect square.
The answer is therefore the number of perfect squares in 1..n. These are 1^2, 2^2, ..., m^2 where m is the largest integer with m^2 <= n, so the answer is floor(sqrt(n)). For n = 0 this gives 0.
Why it is correct
- Each lamp's final state is determined only by the parity of its divisor count.
- The pairing argument shows that parity is odd exactly for perfect squares.
- Counting perfect squares up to
ngivesisqrt(n), which computes the floor of the square root exactly with integers, so there are no floating-point errors even fornup to10^18.
Complexity
- Time:
O(1)arithmetic operations, treating the integer square root as constant time (it is polylogarithmic innin practice). - Space:
O(1).
Reference solution
from math import isqrt
def count_lamps_on(n: int) -> int:
# Lamp j is flipped once per divisor of j. Divisors pair up as (d, j // d),
# so the count is odd only when j is a perfect square (sqrt(j) pairs with
# itself). The lamps left on are exactly the perfect squares in 1..n.
return isqrt(n)