Bulb Switcher

There are n bulbs that are initially off. In round 1 you turn on all the bulbs. In round 2 you toggle every second bulb, in round 3 every third bulb, and so on: in round i you toggle every i-th bulb. Round n toggles only the last bulb. Return the number of bulbs that are on after n rounds.

30:00

Bulb Switcher
medium
Free written solution
Topics
Companies

There are n bulbs that are initially off. In round 1 you turn on all the bulbs. In round 2 you toggle every second bulb, in round 3 every third bulb, and so on: in round i you toggle every i-th bulb. Round n toggles only the last bulb. Return the number of bulbs that are on after n rounds.

Example 1:
Input: {"n":3}
Output: 1
Constraints:
  • 0≤n≤1090 \leq n \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":3}

3 bulbs, all off. In round k every k-th bulb is flipped, for k = 1 to 3. How many are on at the end?

Start: all off● on: 0×k = times flipped
1
2
3
Variables
VariableValue
n3
round-
on0
DepthFunction Call
Stack empty
0/8