Program to count number of on lights flipped by n people in Python

Suppose we have n toggle switches in a room and n people present in that room. They flip switches according to a specific pattern:

  • Person 1 comes and flips all switches (1, 2, 3, 4, ...)
  • Person 2 comes and flips switches that are multiples of 2 (2, 4, 6, 8, ...)
  • Person i comes and flips switches that are multiples of i

We need to find how many switches will be in the ON position after all n people have finished flipping.

Understanding the Problem

Let's trace through an example with n = 5 switches. Initially all switches are OFF (0):

  • Initially: [0, 0, 0, 0, 0]
  • After person 1 (flips 1,2,3,4,5): [1, 1, 1, 1, 1]
  • After person 2 (flips 2,4): [1, 0, 1, 0, 1]
  • After person 3 (flips 3): [1, 0, 0, 0, 1]
  • After person 4 (flips 4): [1, 0, 0, 1, 1]
  • After person 5 (flips 5): [1, 0, 0, 1, 0]

Final result: 2 switches are ON (positions 1 and 4).

Key Insight

A switch at position i will be flipped by every person whose number divides i evenly. For example, switch 6 gets flipped by persons 1, 2, 3, and 6. A switch ends up ON if it's flipped an odd number of times.

The key insight is that only perfect squares have an odd number of divisors. Therefore, only switches at perfect square positions (1, 4, 9, 16, ...) will remain ON.

Solution Using Square Root

The number of ON switches equals the number of perfect squares ? n, which is simply floor(?n):

import math

def count_on_lights(n):
    return int(math.sqrt(n))

# Test with example
n = 5
result = count_on_lights(n)
print(f"For n = {n}, number of ON lights: {result}")

# Test with more examples
test_cases = [1, 4, 9, 16, 25, 100]
for n in test_cases:
    print(f"n = {n}: {count_on_lights(n)} lights ON")
For n = 5, number of ON lights: 2
n = 1: 1 lights ON
n = 4: 2 lights ON
n = 9: 3 lights ON
n = 16: 4 lights ON
n = 25: 5 lights ON
n = 100: 10 lights ON

Solution Using Binary Search

Alternatively, we can use binary search to find the largest integer whose square is ? n:

def count_on_lights_binary_search(n):
    left, right = 0, n
    
    while left <= right:
        mid = left + (right - left) // 2
        
        if mid * mid <= n < (mid + 1) * (mid + 1):
            return mid
        elif n < mid * mid:
            right = mid - 1
        else:
            left = mid + 1
    
    return left - 1

# Test both approaches
n = 5
result1 = count_on_lights(n)
result2 = count_on_lights_binary_search(n)

print(f"Square root method: {result1}")
print(f"Binary search method: {result2}")
Square root method: 2
Binary search method: 2

Comparison

Method Time Complexity Space Complexity Best For
Square Root O(1) O(1) Simple and efficient
Binary Search O(log n) O(1) When sqrt() is not available

Conclusion

The problem reduces to counting perfect squares ? n. The square root method provides an O(1) solution, while binary search offers an alternative O(log n) approach when direct square root calculation isn't available.

Updated on: 2026-03-26T17:36:16+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements