> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Bulb Switcher Python Solution with Tests

> Tested Python solution for LeetCode 319 with 33 pytest cases. Generate a practice environment with lcpy.

LeetCode 319, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), Brainteaser. [View on LeetCode](https://leetcode.com/problems/bulb-switcher/description/).

Generate this problem as a practice environment: tested reference solution, 33 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 319   # by problem number
lcpy gen -s bulb_switcher   # by problem name
```

## Problem

\<p>There are \<code>n\</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.\</p>

\<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the \<code>i\<sup>th\</sup>\</code> round, you toggle every \<code>i\</code> bulb. For the \<code>n\<sup>th\</sup>\</code> round, you only toggle the last bulb.\</p>

\<p>Return \<em>the number of bulbs that are on after \<code>n\</code> rounds\</em>.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/11/05/bulb.jpg)

```
Input: n = 3
Output: 1
Explanation: At first, the three bulbs are [off, off, off].
After the first round, the three bulbs are [on, on, on].
After the second round, the three bulbs are [on, off, on].
After the third round, the three bulbs are [on, off, off].
So you should return 1 because there is only one bulb is on.
```

```
Input: n = 0
Output: 0
```

```
Input: n = 1
Output: 1
```

### Constraints

* 0 \<= n \<= 10\<sup>9\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/bulb_switcher/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/bulb_switcher/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import isqrt


class Solution:
    # Time: O(1)
    # Space: O(1)
    def bulb_switch(self, n: int) -> int:
        return isqrt(n)
```

## Complexity

| Time | Space |
| - | - |
| O(1) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.