Show problems as
Classic problem #319MediumMathBrainteaser

Bulb Switcher

Real-world version: Greenhouse Vent Passes
Tracked in this browser only
Read Bulb Switcher on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A greenhouse roof carries vent_count vents in one long row, numbered 1 through vent_count. Every vent starts shut.

The controller then runs vent_count sweeps over the row. On sweep k it visits each vent whose number is a multiple of k and flips that vent: a shut vent opens and an open vent shuts. Sweep 1 therefore touches every vent, sweep 2 touches vents 2, 4, 6 and so on, up to sweep vent_count, which touches only the last vent.

Return how many vents are open after the final sweep. If vent_count is 0 the roof has no vents and no sweeps are run.

Examples

Example 1

Input
vent_count = 8
Output
2

Sweep 1 opens all eight vents, sweep 2 flips the even-numbered ones, and each later sweep flips its own multiples. Two vents are open once sweep 8 has run.

Example 2

Input
vent_count = 27
Output
5

Five of the twenty-seven vents are open after the twenty-seventh sweep.

Example 3

Input
vent_count = 49
Output
7

Seven of the forty-nine vents are open once every sweep has run.

Constraints

  • 0 <= vent_count <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_open_vents(vent_count: int) -> int:
Java
public int countOpenVents(int ventCount)