Bulb Switcher

medium math brainteaser

Problem

There are n bulbs, all initially OFF. On round i (1-indexed), you toggle every i-th bulb. After n rounds, how many bulbs are ON?

Inputn = 9
Output3
Bulbs 1, 4, 9 — the perfect squares ≤ 9.

def bulb_switch(n):
    return int(n ** 0.5)
function bulbSwitch(n) {
  return Math.floor(Math.sqrt(n));
}
class Solution {
    public int bulbSwitch(int n) {
        return (int) Math.sqrt(n);
    }
}
int bulbSwitch(int n) {
    return (int) sqrt((double) n);
}
Time: O(1) Space: O(1)